Static array initialization

Steven Schveighoffer schveiguy at yahoo.com
Mon Jul 19 04:47:40 PDT 2010


On Sat, 17 Jul 2010 13:15:18 -0400, bearophile <bearophileHUGS at lycos.com>  
wrote:

> I remember Don saying something about this some time ago, but I don't  
> remember what he said and what the conclusion was.
>
> This is a small D2 program that contains the initialization of a small  
> fixed-size array allocated on the stack:
>
>
> import std.c.stdio: printf;
> import std.c.stdlib: atof;
> void main() {
>     double x = atof("1.0");
>     double y = atof("2.0");
>     double[2] arr = [x, y];
>     printf("%f\n", arr[1]);
> }
>
>
> The asm generated by dmd in an optimized build shows two calls to  
> initialize the array. Can such calls be removed in this simple situation?

The first call builds a dynamic array on the heap.

The second call copies the array from the heap to the stack.

The bug should be that it's using the heap at all.

-Steve


More information about the Digitalmars-d mailing list