new T[size] vs .reserve

FG home at fgda.pl
Sat Feb 2 11:49:41 PST 2013


On 2013-02-02 19:53, Namespace wrote:
>> Are you uncomfortable, because it may allocate twice as much space
>> as you need (for bigger color_data)?
>
> Yes, that's the point.
> Sorry, I cannot express myself very well in English.

You're right. It's surprising for anyone used to dealing with std::vector,
that it actually reserves more than you specify.

Another odd thing - when pushing back to a vector its capacity grows:
1, 2, 4, 8, 16, 32 ..., but with D arrays it's like 7, 15, 31, 63...
Why 2**n - 1? What secret data is hidden after the block? ;)


More information about the Digitalmars-d-learn mailing list