Is the storage for a std::vector<T> guaranteed to be contiguous? (from FAQ)

N

Newsgroup - Ann

Hi:

I saw the following codes from the FAQ about the contiguous storage of
vector. I am just wondering how does the implementation of the <vector>
guarantee the contiguous? What if a vector v was defined first as a
5-element vector, and then was increased dramatically but the corresponding
contiguous area has already been occupied by other objects? Maybe sounds
stupid to you gurus, but it does confuse me:( Thanks.

----------------------
#include <vector>
#include "Foo.h" /* get class Foo */

// old-style code that wants an array
void f(Foo* array, unsigned numFoos);

void g()
{
std::vector<Foo> v;
...
f(&v[0], v.size()); ? safe
}
----------------------------
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top