Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Re: reduce c++ vector size

Thread Tools

Re: reduce c++ vector size

Juha Nieminen
Posts: n/a
Rui Maciel <(E-Mail Removed)> wrote:
> Juha Nieminen wrote:
>> (For example in a 32-bit linux system the minimum allocation size is
>> 16 bytes, and every allocation over that will be divisible by 8, with a
>> minimum of 4 bytes in addition to what was explicitly requested. I imagine
>> that in 64-bit linux this is larger.)

> Even if the overhead is larger, your assumption still doesn't make sense. A
> 10-element std::list only starts to require more memory than a 32-element
> std::vector if the overhead of storing each element in a std::list is over
> twice the size of each element.

If the size of the element is, for example, that of one int, then on a
32-bit Linux system each vector element will take 4 bytes, while each
list element takes 16 bytes of memory (each list element will have the
size of the int plus two pointers, ie. 12 bytes, and as said, the minimum
allocation size using the default allocator is 16 bytes).

The memory allocation overhead will apply to the vector as well, of course,
but it applies only once because the vector has only one single block of
memory allocated. Hence the memory taken by the vector will be 32 times 4
plus 8 (because, as said, allocation sizes in Linux are always the requested
amount plus 4, rounded up to the nearest multiple of , which would be
136 bytes.

A std::list of 10 elements, on the other hand, takes 160 bytes of memory in
total (because, as said, each element takes 16 bytes). And this assuming no
memory fragmentation (so that each allocated block is neatly one after
another in memory).

Last time I checked 160 is larger than 136. Hence a vector of 32 elements
*can* take less memory than a list of 10 elements.

(Also, the memory taken by the list can potentially be even larger if
there's memory fragmentation so the allocator cannot place all those
blocks neatly one after another in memory.)
Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: reduce c++ vector size Fraser Ross C++ 0 06-04-2012 10:45 AM
Re: reduce c++ vector size Ian Collins C++ 0 06-04-2012 08:47 AM
Re: reduce c++ vector size Alain Ketterlin C++ 0 06-03-2012 05:52 PM
Free memory allocate by a STL vector, vector of vector, map of vector C++ 8 02-18-2006 12:48 AM
better to compress the jpeg or reduce resolution or reduce pixel size? Mr.Will Digital Photography 8 10-08-2004 03:16 PM