简体   繁体   English

在std :: vector.reserve()之后在std :: vector中重新分配

[英]Reallocation in std::vector after std::vector.reserve()

I have a snippet of code where I first put some values in to a std::vector and then give an address of each of them to one of the objects that will be using them, like this: 我有一段代码,我首先将一些值放入std::vector ,然后将每个值的地址提供给将要使用它们的对象之一,如下所示:

std::vector < useSomeObject > uso;

// uso is filled

std::vector < someObject > obj;

for (int i=0; i < numberOfDesiredObjects; ++i){
    obj.push_back(someObject());
    obj.back().fillWithData();
}
for (int i=0; i < numberOfDesiredObjects; ++i){
    uso[i].setSomeObject(&obj[i]);
}

// use objects via uso vector
// deallocate everything

Now, since I'm sometimes a little bit of a style freak, I think this is ugly and would like to use only 1 for loop, kind of like this: 现在,由于有时我有点风格怪异,所以我认为这很丑陋 ,只想对循环使用1,就像这样:

for (int i=0; i < numberOfDesiredObjects; ++i){
    obj.push_back(someObject());
    obj.back().fillWithData();
    uso[i].setSomeObject(&obj.back());
}

Of course, I can not do that because reallocation happens occasionally, and all the pointers I set became invalid. 当然,我不能这样做,因为重新分配偶尔会发生,并且我设置的所有指针都将变为无效。

So, my question is: I know that std::vector.reserve() is the way to go if you know how much you will need and want to allocate the memory in advance. 因此, 我的问题是:如果您知道需要多少内存并想要提前分配内存,我知道std::vector.reserve()是必经之路。 If I make sure that I am trying to allocate enough memory in advance with reserve() , does that guarantee that my pointers will stay valid? 如果我确定要尝试通过reserve()预先分配足够的内存,那是否可以确保我的指针保持有效?

Thank you. 谢谢。


Sidenote. 边注。 This is a similar question, but there is not an answer to what I would like to know. 是一个类似的问题,但我想知道的答案没有答案。 Just to prevent it from popping up as a first comment to this question. 只是为了防止它作为对此问题的第一条评论而弹出。

This is, in fact, one of the principal reasons for using reserve . 实际上,这是使用reserve主要原因之一。 You are guaranteed that appending to the end of an std::vector will not invalidate iterators, references or pointers to elements in the vector as long as the new size of the vector does not exceed the old capacity. 您可以保证,只要在向量的新大小不超过旧容量的情况下,将其追加到std::vector的末尾不会使std::vector中元素的迭代器,引用或指针无效。

If I make sure that I am trying to allocate enough memory in advance with reserve(), does that guarantee that my pointers will stay valid? 如果我确定要尝试通过reserve()预先分配足够的内存,那是否可以确保我的指针保持有效?

Yes, it guarantees your pointers will stay valid unless: 是的,它保证您的指针将保持有效,除非:

  • The size increases beyond the current capacity or 大小增加到超出当前容量
  • Unless, you erase any elements, which you don't. 除非您擦除所有元素,否则不要删除

The iterator Invalidation rules for an vector are specified in 23.2.4.3/1 & 23.2.4.3/3 as: 向量的迭代器无效规则在23.2.4.3/123.2.4.3/3中指定为:

All iterators and references before the point of insertion are unaffected, unless the new container size is greater than the previous capacity (in which case all iterators and references are invalidated) 插入点之前的所有迭代器和引用均不受影响,除非新的容器大小大于先前的容量(在这种情况下,所有迭代器和引用均无效)

Every iterator and reference after the point of erase is invalidated. 擦除点之后的每个迭代器和引用均无效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM