How many bytes does a vector have?

C

cylin

Dear all,

Using sizeof(vector<TYPE>) will always return 16 bytes.
If I have N elements ( integer ),
the total memory is
sizeof(vector<int>)+sizeof(int)*N,
or sizeof(vector<int>)*N,
or others?

Thanks your help.

Best Regards,
cylin.
 
J

John Harrison

cylin said:
Dear all,

Using sizeof(vector<TYPE>) will always return 16 bytes.
If I have N elements ( integer ),
the total memory is
sizeof(vector<int>)+sizeof(int)*N,
or sizeof(vector<int>)*N,
or others?

There is no guaranteed way of determining how many bytes a vector is using.
sizeof(vector<int>)+sizeof(int)*N,

That should be pretty close, providing N == capacity() not N == size(). The
capacity of a vector is how much storage it has allocated for itself, this
is often more than the size of the vector.

John
 
D

Davide Rossetti

cylin said:
Dear all,

Using sizeof(vector<TYPE>) will always return 16 bytes.
If I have N elements ( integer ),
the total memory is
sizeof(vector<int>)+sizeof(int)*N,
or sizeof(vector<int>)*N,
or others?

isn't it that the vector uses heap allocated memory to store its
elementes?? so sizeof() actually returns the (constant) size of the
container structure, which internally has a pointer to a
heap-allocated chunk of memory...
 
C

cylin

There is no guaranteed way of determining how many bytes a vector is
using.
That should be pretty close, providing N == capacity() not N == size(). The
capacity of a vector is how much storage it has allocated for itself, this
is often more than the size of the vector.

I doubt about the function "reserve".
For example:
A vector<int> iVector, and I add 100 elements to this vector.
iVector.capacity()=128
iVector.size()=100.

If I use iVector.reserve(iVector.size()),
then iVector.capacity()=128.
It seems no use.
 
D

Daniel T.

cylin said:
Using sizeof(vector<TYPE>) will always return 16 bytes.
If I have N elements ( integer ),
the total memory is
sizeof(vector<int>)+sizeof(int)*N,
or sizeof(vector<int>)*N,
or others?

template < typename T >
unsigned long totalRAM( const vector<T>& vec ) {
return sizeof( vec ) + sizeof( T ) * vec.capacity();
}

But why would you care so much?
 
S

Sam Holden

template < typename T >
unsigned long totalRAM( const vector<T>& vec ) {
return sizeof( vec ) + sizeof( T ) * vec.capacity();
}

What if a vector implmentation stored elements inline when
the capacity was small?
 
C

cylin

Daniel T. said:
template < typename T >
unsigned long totalRAM( const vector<T>& vec ) {
return sizeof( vec ) + sizeof( T ) * vec.capacity();
}

But why would you care so much?

Because I want to write some vector objects to disk.
And when I read them from disk, I can use directly.
So I need the exact size.

But for other STL containers, how to do?
I think I can't store such object directly except "vector".
 
D

Daniel T.

cylin said:
Because I want to write some vector objects to disk.
And when I read them from disk, I can use directly.
So I need the exact size.

Ah, now we learn what you *really* want.

For this you want to iterate through the vector and write out/read in
each element. *Don't* try to just dump the vectors memory.

But for other STL containers, how to do?
I think I can't store such object directly except "vector".

Same as above...
 
D

Default User

cylin wrote:

Because I want to write some vector objects to disk.
And when I read them from disk, I can use directly.
So I need the exact size.

What good would that do you? You can't read the binary data back up and
recreate the object from it. Standard containers are likely to use
dynamic memory, so some of its "parts" are pointers to memory areas on
the free store that won't exist later.

You need to worry only about the data, not the vector itself. Vector is
easy because the data itself can be treated like an array (I think
that's in the standard now) so you can write that out as a block into
the file. Later, you read it up into an array and create a new vector
from it. There's no point in trying to "save" the vector part of it,
you only care about the data.

For other containers, you'll need to devise a data serialization scheme.


Brian Rodenborn
 
P

Paul Williams

cylin said:
I doubt about the function "reserve".
For example:
A vector<int> iVector, and I add 100 elements to this vector.
iVector.capacity()=128
iVector.size()=100.

If I use iVector.reserve(iVector.size()),
then iVector.capacity()=128.
It seems no use.

reserve is guarunteed to reserve *at least* the amount you ask for.

Vectors typically double their size when theyre forced to reallocate.

So if you take an empty vector and add elements you get:

E=elemts
S=size
C=capacity

E S C

1 1 1
2 2 2
3 3 4
4 4 4
5 5 8
6 6 8
7 7 8
8 8 8
9 9 16

etc etc

If you take an empty vector and do reserve(10), for example, you would
have E=0, S=0, C=16.....
 

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
474,175
Messages
2,570,945
Members
47,495
Latest member
Jack William

Latest Threads

Top