简体   繁体   English

C ++ std :: vector内存/分配

[英]C++ std::vector memory/allocation

From a previous question about vector capacity , Mr. Bailey said: 根据先前关于载体容量的问题 ,贝利先生说:

In current C++ you are guaranteed that no reallocation occurs after a call to reserve until an insertion would take the size beyond the value of the previous call to reserve. 在当前的C ++中,保证在调用保留之后不会发生重新分配,直到插入的大小超过上一次保留调用的值。 Before a call to reserve, or after a call to reserve when the size is between the value of the previous call to reserve and the capacity the implementation is allowed to reallocate early if it so chooses. 在呼叫保留之前,或者在保留呼叫之后,当大小介于上一次保留呼叫的值和容量之间时,如果允许,则允许实施提前重新分配。

So, if I understand correctly, in order to assure that no reallocation happens until capacity is exceeded, I must do reserve twice? 所以,如果我理解正确,为了确保在超出容量之前不会重新分配,我必须保留两次? Can you please clarify it? 你能澄清一下吗?

I am using vector as a memory stack like this: 我正在使用vector作为这样的内存堆栈:

std::vector<double> memory;
memory.reserve(size);
memory.insert(memory.end(), matrix.data().begin(), matrix.data().end()); // smaller than size
memory.resize(memory.capacity(), 0);

I need to guarantee that reallocation does not happen in the above. 我需要保证在上面不会发生重新分配。

thank you. 谢谢。

ps: I would also like to know if there is a better way to manage memory stack in similar manner other than vector ps:我还想知道是否有更好的方法以类似的方式管理内存堆栈而不是vector

I think you're reading the statement wrong. 我认为你读错了。 Reserve is allowed to set capacity to a larger amount than what you reserved. 允许保留将capacity设置为比您预留的capacity更大的capacity The special language is to allow an implementation to reallocate if you're reserving more than you did the last time, but before you've reached the current capacity. 特殊语言是允许实现重新分配,如果您保留的次数超过上次,但在达到当前容量之前。

It reallocates when it needs more. 它需要更多时重新分配。 You don't need to reserve twice. 您不需要预留两次。 It is a good idea to use reserve if you're doing a lot of inserting and you have good knowledge of the size of what you're storing. 如果您正在进行大量插入并且您对存储的大小有很好的了解,那么使用reserve是个好主意。 It's much faster. 它快得多。

You shouldn't need to call reserve twice. 你不应该两次打电话reserve

Additionally, in the code sample posted, you've only called reserve once. 此外,在发布的代码示例中,您只调用了一次reserve In actuality the code you're pasting affects the size of the vector. 实际上,您粘贴的代码会影响向量的size See 23.3.6.2.11 (C++0x FCD) on effects of void resize(size_type sz, const T& c); 有关void resize(size_type sz, const T& c);影响,请参见23.3.6.2.11(C ++ 0x FCD void resize(size_type sz, const T& c); :

if (sz > size())
    insert(end(), sz-size(), c);
else if (sz < size())
    erase(begin()+sz, end());
else
     ; // do nothing

So, basically, when you call memory.resize(memory.capacity(), 0) , you are, in effect, appending on 0 , memory.capacity() - memory.size() times following your call to memory.insert . 所以,基本上,当你调用memory.resize(memory.capacity(), 0)你是,实际上,附加上0memory.capacity() - memory.size()倍的调用以下memory.insert

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

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