Vectors and memory
I just wanted to know "do vectors store there values in memory consecutively like a normal array from start to end? Or can they be broken up.
By this i just mean in one instance of time, Im not talking about reallocation after the string has been modifyed or anything like that
A normal implementation of a vector will store its elements in a consecutive block of memory, just like a dynamically allocated array. I'm not sure if the standard guarantees this or not, but if you use gcc at least that will be the representation.
The idea of a vector is that it support random access fairly efficiently -- if that's what you need, you can use a vector without really worrying about how it is implemented . . . .
The standard does not guarantee it, but this is considered an oversight in the standard. All realistic implementations of std::vector store their contents in a contiguous array of memory. If it were otherwise, the vector would not be able to satisfy the complexity requirements dictated by the standard.
Originally Posted by Matty_Alan
So you can assume that the contents of the vector occupy a contiguous region of memory from &vec.front() to &vec.back().
Thanks guys, Im mainly just playing with vectors iv'e never really used them before but the general idea that iv'e picked up is that there just an array thats with a little more flexiablity.
I have seen some earlier versions that (depending on certain system constraints and compile switches) would say start with a contiguous block of 64 elements, then if more were needed it could either allocate a larger block and move the old contents to the new location OR it would just allocate a new smaller or equal sized block and just maintain a link between the two depending on the indices requested, saving (temporarily) the time needed to move all of the old data. I am not saying this was right or even still in play but I did see it...
According to my searches, that was true of the early standard of C++, but this oversight was corrected in C++03
Originally Posted by brewbuck
Is std::vector contiguous?