'static' keyword for positional array initialization

grauzone none at example.net
Wed Aug 12 04:32:49 PDT 2009


Ali Cehreli wrote:
> Lars T. Kyllingstad Wrote:
> 
>> I've tried with DMD 2.031, and I can't reproduce this. This works fine 
>> for me:
>>
>>    int[2] static_0 = [ 1, 1 ]:
>>    int[2] static_1 = [ 1:1 ];
>>
>> Where did you put the declarations? I've tried putting them at both 
>> module level and in a class, and both times it compiled without problems.
> 
> Thank you very much for both of your answers. :)
> 
> I hadn't realized that the location of the definitions would make a difference. As you say, both of the lines work for me in the global scope (probably in a class too), but not in main:
> 
> void main()
> {
>     int[2] static_0 = [ 1, 1 ];
>     int[2] static_1 = [ 1:1 ];
> }
> 
> dmd: init.c:431: virtual Expression* ArrayInitializer::toExpression(): Assertion `j < edim' failed.
> 
> The 'static' keyword on the 1:1 line fixes the problem...

The error message is a compiler bug, but AFAIK the code above is not 
valid anyway. The special initializer syntax (apparently called static 
inittializers) for arrays and structs only works for data that's stored 
on the data segment. This applies for global variables, static 
variables, and class member initializers.

It simply doesn't work for normal variables, which allocate their 
storage on the stack.


More information about the Digitalmars-d-learn mailing list