On 04/06/2012 18:47, Rui Maciel wrote:
> Juha Nieminen wrote:
>
>> The overhead will be larger because each element is allocated
>> separately, and the standard memorly allocator needs extra space for
>> each allocated block of memory.
>>
>> (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.
>
> This means that if the minimum allocation size is 16 bytes, if an element is
> 16 bytes then, for your assertion to hold, the overhead for each element in
> a container must be over 34 bytes. And if the element takes more memory
> than that then, for your assertion to hold, the overhead must increase in a
> directly proportional way.
>
> And this is for the corner case where the std::list is maxed out, which is a
> corner case.
>
> So, suffice to say, your assertion is false.
I have made some measurements on my default implementation:
Total memory used by std::vector<int>(32) is 152 bytes, where:
sizeof(int) is 4 bytes
sizeof(vectorInstance) is 24 bytes
size == capacity (asserted)
Total memory used by std::list<int>(10) is 256 bytes, where:
sizeof(int) is 4 bytes
sizeof(listInstance) is 16 bytes
std::vector<int>(32) wins.
On the other hand:
Total memory used by std::vector<unsigned long>(32) is 280 bytes, where:
sizeof(unsigned long) is 8 bytes
sizeof(vectorInstance) is 24 bytes
size == capacity (asserted)
Total memory used by std::list<unsigned long>(10) is 256 bytes, where:
sizeof(unsigned long) is 8 bytes
sizeof(listInstance) is 16 bytes
std::list<unsigned long>(10) wins.
|