=void in struct definition
Jonathan M Davis
newsgroup.d at jmdavisprog.com
Wed Apr 11 07:58:48 UTC 2018
On Wednesday, April 11, 2018 10:45:40 Shachar Shemesh via Digitalmars-d
wrote:
> On 09/04/18 14:22, Jonathan M Davis wrote:
> > On Monday, April 09, 2018 14:06:50 Shachar Shemesh via Digitalmars-d
wrote:
> >> struct S {
> >>
> >> int a;
> >> int[5000] arr = void;
> >>
> >> }
> >>
> >> void func() {
> >>
> >> S s;
> >>
> >> }
> >>
> >> During the s initialization, the entire "S" area is initialized,
> >> including the member arr which we asked to be = void.
> >>
> >> Is this a bug?
> >
> > It looks like Andrei created an issue about it as an enhancement request
> > several years ago:
> >
> > https://issues.dlang.org/show_bug.cgi?id=11331
> >
> > - Jonathan M Davis
>
> Except that issue talks about default constructed objects. My problem
> happens also with objects constructed with a constructor:
>
>
> extern(C) void func(ref S s);
>
> struct S {
> uint a;
> int[5000] arr = void;
>
> this(uint val) {
> a = val;
> }
> }
>
> void main() {
> auto s = S(12);
>
> // To prevent the optimizer from optimizing s away
> func(s);
> }
>
> $ ldc2 -c -O3 -g test.d
> $ objdump -S -r test.o | ddemangle > test.s
>
> 0000000000000000 <_Dmain>:
> }
> }
>
> void main() {
> 0: 48 81 ec 28 4e 00 00 sub $0x4e28,%rsp
> 7: 48 8d 7c 24 04 lea 0x4(%rsp),%rdi
> auto s = S(12);
> c: 31 f6 xor %esi,%esi
> e: ba 20 4e 00 00 mov $0x4e20,%edx
> 13: e8 00 00 00 00 callq 18 <_Dmain+0x18>
> 14: R_X86_64_PLT32 memset-0x4
> a = val;
> 18: c7 04 24 0c 00 00 00 movl $0xc,(%rsp)
> 1f: 48 89 e7 mov %rsp,%rdi
>
> // To prevent the optimizer from optimizing s away
> func(s);
> 22: e8 00 00 00 00 callq 27 <_Dmain+0x27>
> 23: R_X86_64_PLT32 func-0x4
> }
> 27: 31 c0 xor %eax,%eax
> 29: 48 81 c4 28 4e 00 00 add $0x4e28,%rsp
> 30: c3 retq
>
>
> Notice the call to memset.
>
> Shachar
All objects are initialized with their init values prior to the constructor
being called. So, whether an object is simply default-initialized or whether
the constructor is called, you're going to get the same behavior except for
the fact that the constructor would normally do further initialization
beyond the init value. As such, if there's a problem with the
default-initialized value, you're almost certainly going to get the same
problem when you call a constructor.
- Jonathan M Davis
More information about the Digitalmars-d
mailing list