Avoid initializing a struct field.

bearophile bearophileHUGS at lycos.com
Sat May 4 11:20:39 PDT 2013


On Saturday, 4 May 2013 at 18:11:03 UTC, jerro wrote:
> Is there a way to avoid default initializing a struct field in 
> D? The following doesn't seem to work:
>
> struct Foo
> {
>     int[42] buffer = void;
>     int bar;
> }

I think it's supposed to work. If it doesn't work, then I think 
it's a compiler bug.

What code have you used to test it, and what is the result you 
have seen?

With this code:

struct Foo {
     int[42] buffer;
     int bar;
}
int main() {
     Foo f;
     return f.bar;
}


DMD gives this code if I use void and if I don't use it:


// With void:
Dmain:
     sub ESP, 0ACh
     mov ECX, 02Bh
     push ESI
     mov ESI, offset FLAT:_D4temp3Foo6__initZ
     push EDI
     lea EDI, 8[ESP]
     rep
     movsd
     mov EAX, 0B0h[ESP]
     pop EDI
     pop ESI
     add ESP, 0ACh
     ret


// Without void:
Dmain:
     sub ESP, 0ACh
     mov ECX, 02Bh
     xor EAX, EAX
     push EDI
     lea EDI, 4[ESP]
     rep
     stosd
     mov EAX, 0ACh[ESP]
     pop EDI
     add ESP, 0ACh
     ret


Bye,
bearophile


More information about the Digitalmars-d-learn mailing list