473,386 Members | 1,962 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

How many bytes does a vector have?

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
9 2328

"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
"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
> 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
"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
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

"Daniel T." <po********@eathlink.net> ¼¶¼g©ó¶l¥ó·s»D
: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
"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Steven T. Hatton | last post by:
I'm trying to parse an ELF file to build a human readable representation. I know it's been done before, and there are tools such as objdump, nm, readelf, c++filt, etc. I can look at the source to...
19
by: James Harris | last post by:
My K&R 2nd ed has in the Reference Manual appendix, A7.4.8 sizeof yields the number of BYTES required to store an object of the type of its operand. What happens if C is running on a machine that...
7
by: Alex | last post by:
Hello people, I have a code written in JAVA that creates field of bytes: byte uuid = new byte; Now I have to translate this line into C++. I'm working in VS 6.0. (unfortunately I have to)....
13
by: Pep | last post by:
I have to interface to an older library that uses strings and there is no alternative. I need to pass a string that is padded with null bytes. So how can I append these null bytes to the...
2
by: fineman | last post by:
Hi all, I want to get a 64bit(8 bytes) Encrypt result use DES class in the VS2005. Though I encrypt data is 64bit(8 bytes), but DES return encrypt result that always is 128bit(16 bytes), I don't...
6
by: Wes | last post by:
I'm running FreeBSD 6.1 RELEASE #2. The program is writting in C++. The idea of the program is to open one file as input, read bytes from it, do some bitwise operations on the bytes, and then...
0
by: chet | last post by:
I am reading 16 bytes from a BIN file at a time and stroing inside a vector. Vectors are made up of char *. Now when im iterating through the vector im stroring those 16 bytes inside a buffer. My...
2
by: Gus007 | last post by:
Hi all, I am new in this community but already need support as below: Hope you guys could help! The idea was: The user input some text in the program and then I put the information inserted in...
2
by: Lambda | last post by:
The code is simple: // Token.h #ifndef TOKEN_H #define TOKEN_H #include <vector> #include <string> class Token
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.