Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

在vector的fill_init代码中,为什么有 const size_type init_size = mystl::max(static_cast<size_type>(16), n); 的动作呢 #150

Open
Misng opened this issue Jun 10, 2024 · 1 comment

Comments

@Misng
Copy link

Misng commented Jun 10, 2024

// fill_init 函数
template <class T>
void vector<T>::
fill_init(size_type n, const value_type& value)
{
  const size_type init_size = mystl::max(static_cast<size_type>(16), n);
  init_space(n, init_size);
  mystl::uninitialized_fill_n(begin_, n, value);
`}`

中,为什么要有 const size_type init_size = mystl::max(static_cast<size_type>(16), n); 的动作呢

@frederick-vs-ja
Copy link
Contributor

frederick-vs-ja commented Aug 14, 2024

估计是为了减少内存碎片,避免过于小的单次动态分配。

但是我觉得当 n == 0 时应该直接退出并且不进行动态分配。 @Alinshans

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants