tags:

views:

130

answers:

5

Possible Duplicate:
Are std::vector elements guaranteed to be contiguous?

does std::vector always contain the data in sequential memory addresses as array[number]?

+1  A: 

Yes. It does not, however, allocate on the stack.

Billy ONeal
Unless of course you passed it an allocator that does just that.
Noah Roberts
@Noah Roberts: I don't see how creation of such an allocator is possible, given that such an allocator's data has to persist past the allocator's `allocate` method.
Billy ONeal
+4  A: 

Yes. Given:

std::vector<int> arr;

you can be sure that

&arr[0]

will give you a pointer to a contiguous array of ints that can (for instance) be passed legacy APIs.

Steve Fallows
No, you can't. The standard allocator allocates memory with new. Allocated memory is *not* an array. The whole premise is wrong. I might be picky, but it's still true. Allocated memory is not an array. They simply share semantics.
Mads Elvheim
Brian R. Bondy
Billy ONeal
@Billy: OK fair enough, I have just seen it the .front way everywhere before.
Brian R. Bondy
@Mads: What are you talking about? The standard is very clear about this issue: "Entities created by a *new-expression* have dynamic storage duration [...] If the entity is a nonarray object, the *new-expression* returns a pointer to the object created. If it is an array, the *new-expression* returns a pointer to the initial element **of the array**."
FredOverflow
FredOverflow: A pointer is not an array. They share some semantics, but that's all. Because of the standard allocator, I would not call Vector for an array. Is that better? :)
Mads Elvheim
+4  A: 

For all types except bool, the standard requires the elements are contiguous in memory:

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

Do keep in mind that std::vector<bool> has special requirements and is not the same as an array of bool.

R Samuel Klatchko
A: 

Yes, the vector use sequential memory for all the elements stored. That means random access is almost as fast as normal array's index.

But vector allocate its memory in heap instead of stack. So it could be less less efficient in some cases.

Shuo
A: 

When you use the standard allocator, you can be sure that the allocated memory is continuous when using std::vector. In other words, foo[n+1] is the next element in the sequence after foo[n]. But std::vector is not an array, just like

int* blah = new int[100];

is not an array. But

int blah[100];

made on the stack is an array. Pointers to allocated memory and arrays just happen to share semantics. They are not equal per the standard, so don't confuse the two.

Mads Elvheim
This is not correct. `new int[100]` does indeed create an array. "Entities created by a new-expression have dynamic storage duration... If [the entity] is an array, the new-expression returns a pointer to the initial element of the array" (C++03 §5.3.4/1). `blah` is not an array, but the object created by `new int[100]` is.
James McNellis
Ah, right. Thanks for the correction :-)
Mads Elvheim