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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top