473,324 Members | 2,456 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,324 software developers and data experts.

vector memory cost

dj
This is the relevant part of my code:

//point A
std::vector<int> x;
//point B
x.reserve(100);
//point C
x.reserve(200);
//point D

At points A, B, C, and D I query for free physical memory (this is
platform dependent and I use GlobalMemoryStatus under Windows, but is
irrelevant here). The differences are what interests me because they
should tell me something about the memory cost of std::vector.

This is what I get:
B - A = 0 bytes, i.e. the vector declaration costs nothing
C - B = 4096, i.e. 100 integers cost 4 KB
D - C = 0, i.e. 200 integers cost the same as 100 integers

None of this makes much sense to me. Is this compiler dependent? At
point C x.capacity() reports 100, and at point D it reports 200. So
where are the catches?
Jun 7 '06 #1
2 1964

dj skrev:
This is the relevant part of my code:

//point A
std::vector<int> x;
//point B
x.reserve(100);
//point C
x.reserve(200);
//point D

At points A, B, C, and D I query for free physical memory (this is
platform dependent and I use GlobalMemoryStatus under Windows, but is
irrelevant here). The differences are what interests me because they
should tell me something about the memory cost of std::vector.
Why? This is what I get:
B - A = 0 bytes, i.e. the vector declaration costs nothing
C - B = 4096, i.e. 100 integers cost 4 KB
D - C = 0, i.e. 200 integers cost the same as 100 integers

None of this makes much sense to me. Is this compiler dependent? Yes. At
point C x.capacity() reports 100, and at point D it reports 200. So
where are the catches?

The catch is that nothing mandates the C++ library to allocate memory
from the operating system. Most likely such allocations are made in
larger - not one char at the time.

/Peter

Jun 7 '06 #2
dj wrote:
This is what I get:
B - A = 0 bytes, i.e. the vector declaration costs nothing C - B = 4096,
i.e. 100 integers cost 4 KB D - C = 0, i.e. 200 integers cost the same as
100 integers


You have detected the Win32 page size; 4,096 bytes.

Memory is hierarchical. A C++ program collects new[] blocks from a
freestore, which is a CRT memory buffer within greater OS buffers, which
use pages as their increment. The benefit is hardware can swap pages, and
can reroute pointers from virtual to absolute pages, operating efficiently
on powers of two. So when you allocate one array, the OS gives your CRT a
new page to play in. Further allocations work in this page, until the CRT
fills it up.

The best newsgroup for your further quest into memory will be
news:microsoft.public.vc.language . This newsgroup is only qualified to
discuss raw C++, which doesn't define any of this stuff.

--
Phlip
Jun 7 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: cylin | last post by:
Dear all, We know that a vector can increase its capacity. Does it mean that system will allocate more memory to fit the value of capacity? If yes, then we maybe cost memory if capacity is...
34
by: Adam Hartshorne | last post by:
Hi All, I have the following problem, and I would be extremely grateful if somebody would be kind enough to suggest an efficient solution to it. I create an instance of a Class A, and...
13
by: Richard | last post by:
vector<char*> m_Text; m_Text.resize(1); char* foo = "FOO"; char* bar = "BAR"; char* foobar = (char*)malloc(strlen(foo) + strlen(bar) + 1); if (foobar) { strcpy(foobar, foo); strcat(foobar,...
17
by: Havatcha | last post by:
Does anyone have a benchmark for the processing overhead of the STL Vector class, vs a C style array? I would dearly love to use Vectors, but am paranoid about slowing my real-time code down. Can...
9
by: kathy | last post by:
I am using std::vector in my program: func() { std::vector <CMyClass *> vpMyClass; vpMyClass.push_back(new CMyClass()); vpMyClass.push_back(new CMyClass()); vpMyClass.push_back(new...
32
by: zl2k | last post by:
hi, c++ user Suppose I constructed a large array and put it in the std::vector in a function and now I want to return it back to where the function is called. I can do like this: ...
13
by: smp | last post by:
Does anyone know why making a vector of pointers is so much less efficient than a vector of objects? For a simple example: int num = 20; vector<int*v_int_ptr; v_int_ptr.reserve(num); ...
8
by: ejack | last post by:
Hello: This is my first time here so I hope I am doing this correctly. I am trying to push a vector object into a vector (I think). Here is my code (header file first) but it's only part of the...
29
by: ab2305 | last post by:
Does standard mandates that the reserve call should initialize a container's values to its defaults, hence vector<intintVec; intVec.reserve(100) would make intVec=intVec....intVec=0 ? Thanks
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.