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 );
Community
  • 1
  • 1
cpp_noname
  • 2,031
  • 3
  • 17
  • 30
  • 7
    What's the question? What exactly is the problem you'd like us to help you solve? – Doug T. Sep 30 '11 at 10:39
  • 1
    ^I just love such replies. Always gives you the feeling that you are being interrogated by the cops :P – Sadique Sep 30 '11 at 10:40
  • I get the feeling he didn't read the question title and was relying on the question description. – Ayjay Sep 30 '11 at 10:44
  • @Ayjay, my fault for fixing the title... – Roddy Sep 30 '11 at 10:46
  • 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. – cpp_noname Sep 30 '11 at 10:46
  • @SteveJessop, Agreed, I thought it was a real question, just not exactly phrased as one :( – Roddy Sep 30 '11 at 10:47
  • @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. – PlasmaHH Sep 30 '11 at 10:48
  • @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. – Sadique Sep 30 '11 at 10:48
  • 1
    @takwing: Check the answers to the question that steve has linked to. In short, the C++ standard defines vectors to be contiguous. – Roddy Sep 30 '11 at 10:50
  • 2
    @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. – Steve Jessop Sep 30 '11 at 10:50
  • @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. – Steve Jessop Sep 30 '11 at 10:55

3 Answers3

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

Roddy
  • 66,617
  • 42
  • 165
  • 277
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().

Patrick
  • 3,790
  • 1
  • 16
  • 12
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.

Ayjay
  • 3,413
  • 15
  • 20