473,408 Members | 1,699 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,408 software developers and data experts.

stl_vector allocation

Hi,

I have not coded in C++ for some years, but generic programming is
bringing me back to it. Unfortunately, some of strange behavior is still
disconcerting. I'd like to know what my language does, so I appreciate
any help regarding this from C++ gods and goddesses...

I have the following riddle regarding STL vector's allocation policy. In
the following code, I'm allocating a vector of size 1:

#include<vector>
int main() {
std::vector<int> v(1);
return 0;
}

I compile this with g++ (gcc version 3.2.2.)

Now, running valgrind (latest version) on it, I get that (snip):

still reachable: 320 bytes in 1 blocks.

In the backtrace, you can see that it's a 'new' from stl_vector. So, I
assume it's doing some sort of specialized allocation for future. I can
show this to be true by allocating any number of vectors of size 1, and I
always get 320. In fact, the later vectors can be of larger size.

Changing the size of the vector to 10 instead of 1, I get:

still reachable: 1600 bytes in 1 blocks.

OK, this is strange, but still, maybe explainable. Let's change the size
of the vector to 100. Running valgrind, I get (and here's a bigger snip):

==14455== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 19 from 1)
==14455== malloc/free: in use at exit: 0 bytes in 0 blocks.
==14455== malloc/free: 1 allocs, 1 frees, 400 bytes allocated.
==14455== For counts of detected errors, rerun with: -v
==14455== No malloc'd blocks -- no leaks are possible.

So, somehow, the policy changes. It's all on the stack and now, I
don't have any "reachable" allocation left.

I'd appreciate if anyone can explain how the vector allocation results in
this behavior.

Thank you. Please CC your post to me.

Afra

Jul 23 '05 #1
4 1859

"Afra Zomorodian" <af**@cs.stanford.edu> wrote in message
news:Pi**************************************@xeno n.Stanford.EDU...
Hi,

I have not coded in C++ for some years, but generic programming is
bringing me back to it. Unfortunately, some of strange behavior is still
disconcerting. I'd like to know what my language does, so I appreciate
any help regarding this from C++ gods and goddesses...

I have the following riddle regarding STL vector's allocation policy. In
the following code, I'm allocating a vector of size 1:

#include<vector>
int main() {
std::vector<int> v(1);
return 0;
}

I compile this with g++ (gcc version 3.2.2.)

Now, running valgrind (latest version) on it, I get that (snip):

still reachable: 320 bytes in 1 blocks.

In the backtrace, you can see that it's a 'new' from stl_vector. So, I
assume it's doing some sort of specialized allocation for future. I can
show this to be true by allocating any number of vectors of size 1, and I
always get 320. In fact, the later vectors can be of larger size.

Changing the size of the vector to 10 instead of 1, I get:

still reachable: 1600 bytes in 1 blocks.

OK, this is strange, but still, maybe explainable. Let's change the size
of the vector to 100. Running valgrind, I get (and here's a bigger snip):

==14455== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 19 from 1)
==14455== malloc/free: in use at exit: 0 bytes in 0 blocks.
==14455== malloc/free: 1 allocs, 1 frees, 400 bytes allocated.
==14455== For counts of detected errors, rerun with: -v
==14455== No malloc'd blocks -- no leaks are possible.

So, somehow, the policy changes. It's all on the stack and now, I
don't have any "reachable" allocation left.

I'd appreciate if anyone can explain how the vector allocation results in
this behavior.


Most if not all implementations of std::vector do indeed allocate more
space than is immediately needed. This is in the interest of performance.
Exactly how much is initially allocated and subsequently allocated when
more memory is needed is left to the implementation. All the performance
requirements made by the standard are formulaic, not specified in actual
time or space.

-Mike
Jul 23 '05 #2
Hi again,

Of course, I understand the difference between "standard" and
"implementation".

So, let me be more precise: Can someone explain the behavior of _this_
implementation?

The question is still valid as this implementation is widely used. I'd
like to know the rationale behind the strange behavior.

Afra

Jul 23 '05 #3

"Afra Zomorodian" <af**@cs.stanford.edu> wrote in message
news:Pi**************************************@xeno n.Stanford.EDU...
Hi again,

Of course, I understand the difference between "standard" and
"implementation".

So, let me be more precise: Can someone explain the behavior of _this_
implementation?
Someone probably can, but not here. Here we only discuss
standard C++, not specific implementations.

The question is still valid
Not valid for comp.lang.c++
as this implementation is widely used.
That's not what dictates topicality here.
I'd
like to know the rationale behind the strange behavior.


Try a newsgroup, mailing list, web site, etc. dedicated
to your implementation.

Purpose of comp.lang.c++ :
http://www.slack.net/~shiva/welcome.txt

-Mike
Jul 23 '05 #4
Afra Zomorodian wrote:
Hi again,

Of course, I understand the difference between "standard" and
"implementation".

So, let me be more precise: Can someone explain the behavior of _this_
implementation?

The question is still valid as this implementation is widely used. I'd
like to know the rationale behind the strange behavior.

Afra

Though the implementation is indeed off topic, there's a simple
solution: Read. The. Code.

HTH,
--ag

--
Artie Gold -- Austin, Texas
http://it-matters.blogspot.com (new post 12/5)
http://www.cafepress.com/goldsays
Jul 23 '05 #5

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

Similar topics

6
by: kwijibo28 | last post by:
Hi all, I've got a simple question regarding stl containers. Consider this code: std::vector<float> foo; foo.resize(100); How do I know if memory allocation was successful? The resize...
6
by: chris | last post by:
Hi all, I need to know, what is the difference between dynamic memory allocation, and stack allocation ? 1. If I have a class named DestinationAddress, when should I use dynamic memory...
66
by: Johan Tibell | last post by:
I've written a piece of code that uses sockets a lot (I know that sockets aren't portable C, this is not a question about sockets per se). Much of my code ended up looking like this: if...
11
by: toton | last post by:
Hi, I have little confusion about static memory allocation & dynamic allocation for a cluss member. I have class like class Bar{ public: explicit Bar(){ cout<<"bar default"<<endl; }
24
by: Ken | last post by:
In C programming, I want to know in what situations we should use static memory allocation instead of dynamic memory allocation. My understanding is that static memory allocation like using array...
1
by: Peterwkc | last post by:
Hello all expert, i have two program which make me desperate bu after i have noticed the forum, my future is become brightness back. By the way, my problem is like this i the first program was...
14
by: vivek | last post by:
i have some doubts on dynamic memory allocation and stacks and heaps where is the dynamic memory allocation used? in function calls there are some counters like "i" in the below function. Is...
158
by: jacob navia | last post by:
1: It is not possible to check EVERY malloc result within complex software. 2: The reasonable solution (use a garbage collector) is not possible for whatever reasons. 3: A solution like the...
10
by: swornavidhya.mahadevan | last post by:
Which allocation (Static / Dynamic) is suitable for the situation when we are trying to allocate for a overloaded memory when the memory is full and no space to allocate. What will happen if both...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
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...
0
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...
0
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...

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.