Array allocations [was: Re: What does C++...]

bearophile bearophileHUGS at lycos.com
Sat Jul 30 09:46:46 PDT 2011


Peter Alexander:

> I also posted my thoughts, although I think I was a bit more negative 
> towards D than you were :-)

I think your answer is among the best of that thread.
Regarding this you have written:

> int[3] a = [1, 2, 3]; // in D, this allocates then copies
> int a[3] = {1, 2, 3}; // in C++, this doesn't allocate
> 
> Apparently, to avoid the allocation in D, you must do:
> 
> static const int[3] staticA = [1, 2, 3]; // in data segment
> int[3] a = staticA; // non-allocating copy
> 
> These little 'behind your back' allocations are good examples of my previous two points.

Memory allocations caused by this, inside an inner loop, have given me performance troubles.
I presume this problem has solutions. Isn't it possible to add a small optimization to DMD to avoid this problem?

Bye,
bearophile


More information about the Digitalmars-d mailing list