| |
![Paul Backus's Gravatar profile Gravatar of Paul Backus](//www.gravatar.com/avatar/90b2b8ae65091e8b6a2cdbcd9d312e26?d=identicon&s=32) | Posted by Paul Backus in reply to Ali Çehreli | Permalink Reply |
|
Paul Backus ![Paul Backus's Gravatar profile Gravatar of Paul Backus](//www.gravatar.com/avatar/90b2b8ae65091e8b6a2cdbcd9d312e26?d=identicon&s=80)
Posted in reply to Ali Çehreli
| On Tuesday, 23 August 2022 at 21:53:35 UTC, Ali Çehreli wrote:
> On 8/23/22 14:40, Dave P. wrote:
>
> >> And yes, 0 happens to be a valid garbage value.
> >
> > The default value for a struct imported via importC is all
> bits 0.
>
> Steve said the same thing but I don't see it on the ImportC page (yet?):
>
> https://dlang.org/spec/importc.html
>
> I wonder why D would do that.
Global and static variables in C are initialized to all-zero bits, regardless of type. Here's the relevant paragraph from the C11 standard:
> If an object that has automatic storage duration is not initialized explicitly, its value is indeterminate. If an object that has static or thread storage duration is not initialized explicitly, then:
>
> if it has pointer type, it is initialized to a null pointer;
> if it has arithmetic type, it is initialized to (positive or unsigned) zero;
> if it is an aggregate, every member is initialized (recursively) according to these rules, and any padding is initialized to zero bits;
> if it is a union, the first named member is initialized (recursively) according to these rules, and any padding is initialized to zero bits;
Source: http://port70.net/~nsz/c/c11/n1570.html#6.7.9p10
Presumably this behavior was implemented in an attempt to conform to this part of the C standard.
|