Are C++ Vectors always contiguous? [duplicate]
Asked Answered
P

3

2

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( &partials[0] , partials.size() , mpi_partial , 0, 
         DALG_ELIMINATE_REQ_MSG ,MPI_COMM_WORLD , &status );
Propound answered 30/9, 2011 at 10:36 Comment(11)
What's the question? What exactly is the problem you'd like us to help you solve?Lafond
^I just love such replies. Always gives you the feeling that you are being interrogated by the cops :PInland
I get the feeling he didn't read the question title and was relying on the question description.Bounty
@Ayjay, my fault for fixing the title...Rumanian
Hi Dough T. I am just not sure about that assumption those people claim to hold true. I don't want to risk using vectors instead of conventional arrays just to find out later that my software breaks down because the vector has grown too large and starts to randomly spill elements in memory.Propound
@SteveJessop, Agreed, I thought it was a real question, just not exactly phrased as one :(Rumanian
@takwing: The question linked to in the comment has a quote of the standard as answer, what more would you want? If you think that an implementation does not adhere to the standard, you will have to check the implementation yourself, as you would probably also have doubts when we assure you that all implementations do adhere to the standard.Nader
@Steve Jessop - I really appreciate what you said sir, but frankly i don't see consistency among many SO members with high reps, at times they have closed valid questions needlessly. The users should be consistent in these regards.Inland
@takwing: Check the answers to the question that steve has linked to. In short, the C++ standard defines vectors to be contiguous.Rumanian
@takwing: vector doesn't "randomly spill elements", but the whole thing is relocated if necessary. So whether this usage is safe depends on what you mean by "grows" - if you mean that in future your program will use bigger sizes than it does today, fine. If you mean that you're calling resize on it while MPI is holding the pointer, not fine.Maxine
@Acme: A certain amount of inconsistency is inevitable: SO users don't have to memorise the rules; the rules are subjective and change from time to time without users being notified; mistakes of comprehension happen; close votes can't be removed if the question is clarified. If a valid question gets closed then that's a shame, but people can sneak answers into comments, vote to reopen, or ask the question again later when different people are online. SO works well, but not perfectly.Maxine
R
6

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...

Rumanian answered 30/9, 2011 at 10:40 Comment(0)
S
3

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().

Silma answered 30/9, 2011 at 10:46 Comment(0)
B
1

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.

Bounty answered 30/9, 2011 at 10:42 Comment(1)
It explicitly requires the vector to use a continguous layout, actually.Kingston

© 2022 - 2024 — McMap. All rights reserved.