开发者

Are C++ Vectors always contiguous? [duplicate]

This question already has answers here: Closed 11 years ago.

Possible Duplicate:

Are std::vector elements guaranteed to be contiguous?

I have come across a technique in which people use a vector in C++ to receive or send data for MPI operations as it is said to store elements contiguously in memory.

However, I remain skeptical of whether this approach would remain robust for a vector of any size, especially when the vector grows to a certain size, where this assumption could break down.

Below is an example of what I am talking about :

MPI_Recv开发者_Python百科( &partials[0] , partials.size() , mpi_partial , 0, 
         DALG_ELIMINATE_REQ_MSG ,MPI_COMM_WORLD , &status );


Yes, C++ vectors are always contiguous, regardless of size.

But that doesn't mean that they don't move around in memory as you shrink or expand them...


The C++ working draft ( www.open-std.org/jtc1/sc22/wg21/docs/papers/2010/n3126.pdf ) says at 23.4.1:

The elements of a vector are stored contiguously, meaning that if v is a vector where T is some type other than bool, then it obeys the identity &v[n] == &v[0] + n for all 0 <= n < v.size().


Basically, yes. All implementations I know of are, and the standard requires vector's to have O[1] lookup which basically requires a contiguous block of memory.

Standard "you shouldn't rely on implementation details" disclaimer.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜