new T[size] vs .reserve

FG home at fgda.pl
Sat Feb 2 10:38:28 PST 2013


On 2013-02-02 19:01, Namespace wrote:
> Example:
>
> struct Color {
> public:
>      ubyte[4] colors;
> }
>
> ubyte[] data = new ubyte[color_data.length * 4]; // enough storage
> foreach (ref const Color col; color_data) {
>      data ~= col.colors;
> }

Sorry, but what is the point of data having only 4 bytes reserved
at the beginning? What is wrong with this:

     ubyte[] data;
     data.reserve(color_data.length * 4);
     foreach (ref const Color col; color_data)
         data ~= col.colors;

Are you uncomfortable, because it may allocate twice as much space
as you need (for bigger color_data)?


More information about the Digitalmars-d-learn mailing list