Vector

R

Rakesh

Hi All,

Assume I declared a vector of char * .

vector<string> x[100]

How the memory is allocated ?

If I push a string of 100 bytes .

How the memory allocation changes ?

After one push if I make another 50 pushes ( Of 100, 50, 250,
..... 10, 20, ..... ) consecutively how the memory allocation of the
vector changes

If I pop one element from the vector how the memory of the
vector changes .

If I pop consecutively 50 strings how the memory of the vector
changes ?

Thanks
Rakesh
 
J

Jon Bell

Assume I declared a vector of char * .

vector<string> x[100]

First, 'string' is not the same thing as 'char *'.
Second, assuming you want to declare a vector with 100 elements, use
parentheses instead of square brackets:

vector said:
How the memory is allocated ?

If you mean, "are the 100 elements allocated in a contiguous chunk of
memory", the answer is "yes".
If I push a string of 100 bytes .

How the memory allocation changes ?

It depends on the implementation of the particular compiler that you're
using. A *common* method is to double the memory allocation of the
vector, so that the next 100 times you do a push_back() it doesn't have to
allocate more memory. When you reach 200 elements, a push_back() doubles
the memory allocation again, etc.
After one push if I make another 50 pushes ( Of 100, 50, 250,
.... 10, 20, ..... ) consecutively how the memory allocation of the
vector changes

Assuming the model described above, the memory allocation of the vector
doesn't change.
If I pop one element from the vector how the memory of the
vector changes .

Again, it depends on the implementation. I don't know whether in practice
any implementations actually reduce the memory allocation of the vector in
this case, or in the following one. I wouldn't rely on it happening.
 
J

John Harrison

Rakesh said:
Hi All,

Assume I declared a vector of char * .

vector<string> x[100]

How the memory is allocated ?

Implementation defined.
If I push a string of 100 bytes .

How the memory allocation changes ?

Implementation defined.
After one push if I make another 50 pushes ( Of 100, 50, 250,
.... 10, 20, ..... ) consecutively how the memory allocation of the
vector changes

Implementation defined.
If I pop one element from the vector how the memory of the
vector changes .


Implementation defined.
If I pop consecutively 50 strings how the memory of the vector
changes ?

Implementation defined.

If you really want to know the answer to these things the only way is to
look at the code for you implementation, look in the <vector> header file.
You might be interested in the capacity and reserve member function which
allow you some small control over the memory allocation of a vector.

However the STL requires that push_back operates in amortized constant time,
which AFAIK pretty much forces an implementer to use a strategy of geometric
growth. Things means that when the vector runs out of room the capacity of
the vector grows by a constant multiple. For instance it might double in
capacity on each reallocation.

john
 

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
474,183
Messages
2,570,968
Members
47,518
Latest member
TobiasAxf

Latest Threads

Top