473,804 Members | 3,019 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How many bytes does a vector have?

Dear all,

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

Thanks your help.

Best Regards,
cylin.
Jul 22 '05 #1
9 2404

"cylin" <cy***@avant.co m.tw> wrote in message
news:2p******** ****@uni-berlin.de...
Dear all,

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

There is no guaranteed way of determining how many bytes a vector is using.
sizeof(vector<i nt>)+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.co m.tw> wrote in message news:<2p******* *****@uni-berlin.de>...
Dear all,

Using sizeof(vector<T YPE>) will always return 16 bytes.
If I have N elements ( integer ),
the total memory is
sizeof(vector<i nt>)+sizeof(int )*N,
or sizeof(vector<i nt>)*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.capacit y()=128
iVector.size()= 100.

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


Jul 22 '05 #4
"cylin" <cy***@avant.co m.tw> wrote:
Using sizeof(vector<T YPE>) will always return 16 bytes.
If I have N elements ( integer ),
the total memory is
sizeof(vector<i nt>)+sizeof(int )*N,
or sizeof(vector<i nt>)*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********@eat hlink.net> wrote:
"cylin" <cy***@avant.co m.tw> wrote:
Using sizeof(vector<T YPE>) will always return 16 bytes.
If I have N elements ( integer ),
the total memory is
sizeof(vector<i nt>)+sizeof(int )*N,
or sizeof(vector<i nt>)*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********@eat hlink.net> ¼¶¼g©ó¶l¥ó·s»D
:po************ *************** ***@news02.east .earthlink.net. ..
"cylin" <cy***@avant.co m.tw> wrote:
Using sizeof(vector<T YPE>) will always return 16 bytes.
If I have N elements ( integer ),
the total memory is
sizeof(vector<i nt>)+sizeof(int )*N,
or sizeof(vector<i nt>)*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.co m.tw> wrote:
"Daniel T." <po********@eat hlink.net> wrote:
"cylin" <cy***@avant.co m.tw> wrote:
Using sizeof(vector<T YPE>) will always return 16 bytes.
If I have N elements ( integer ),
the total memory is
sizeof(vector<i nt>)+sizeof(int )*N,
or sizeof(vector<i nt>)*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.capacit y()=128
iVector.size()= 100.

If I use iVector.reserve (iVector.size() ),
then iVector.capacit y()=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
1983
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 see how they are implemented. In most cases, that means reading C, not C++. I do have an example of C++ code that works. I thought it might be a good idea to take a different approach than that code takes. Rather than processing the data...
19
2120
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 addresses larger words only? Shouldn't sizeof be defined to return the smallest number of 'storage units' required to store an object of the type of its operand? As a general point, is there a guide to what aspects of C would fail if run on a...
7
14596
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). Is there any class in MFC framework that I can use like bytes? Or I
13
6130
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 std::string? Yes I know it would be better to use something like a vector but I do not have that option. Yes I know that I will not be able to use std::string.c_str() but will instead have to use std:;string.getData().
2
3183
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 know why? How to get a 64bit(8 bytes) encrypt result using DES class in the VS2005?
6
2480
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 write them to this second file. However, when the second file is 15360 bytes long, the program dies with a "Segmentation Fault (core dumped)" error! I checked with gdb, and it says the last function to run was memcpy() from libc, which would...
0
1083
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 ultimate aim is to store entire BIN file into a SAFEARRAY. But before i need to transfer to a VARIANT. im running a for loop to transfer 16 bytes 1 by1 from buffer to VARIANT. My question is whether variant copies 1 by1 or takes complete 16 bytes...
2
3507
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 a char vector. int c; char s;
2
13074
by: Lambda | last post by:
The code is simple: // Token.h #ifndef TOKEN_H #define TOKEN_H #include <vector> #include <string> class Token
0
9705
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10564
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10308
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10073
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9134
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7609
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6846
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5645
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3806
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.