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 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
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
"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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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;
}
|
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...
|
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...
|
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...
|
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...
|
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...
|
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
|
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...
|
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,...
|
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: 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...
|
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...
|
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...
|
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...
|
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...
| |