By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,503 Members | 1,608 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,503 IT Pros & Developers. It's quick & easy.

How many bytes does a vector have?

P: n/a
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.
Jul 22 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a

"cylin" <cy***@avant.com.tw> wrote in message
news:2p************@uni-berlin.de...
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
Jul 22 '05 #2

P: n/a
"cylin" <cy***@avant.com.tw> wrote in message news:<2p************@uni-berlin.de>...
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...
Jul 22 '05 #3

P: n/a
> 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.


Jul 22 '05 #4

P: n/a
"cylin" <cy***@avant.com.tw> wrote:
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?
Jul 22 '05 #5

P: n/a
On Thu, 02 Sep 2004 01:54:15 GMT, Daniel T. <po********@eathlink.net> wrote:
"cylin" <cy***@avant.com.tw> wrote:
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();
}


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

--
Sam Holden
Jul 22 '05 #6

P: n/a

"Daniel T." <po********@eathlink.net> glsD
:po******************************@news02.east.eart hlink.net...
"cylin" <cy***@avant.com.tw> wrote:
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?


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".
Jul 22 '05 #7

P: n/a
"cylin" <cy***@avant.com.tw> wrote:
"Daniel T." <po********@eathlink.net> wrote:
"cylin" <cy***@avant.com.tw> wrote:
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?


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...
Jul 22 '05 #8

P: n/a
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
Jul 22 '05 #9

P: n/a
cylin wrote:
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.


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.....
Jul 22 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.