Why is char initialized to 0xFF ?
James Blachly
james.blachly at gmail.com
Sun Jun 9 16:56:53 UTC 2019
On 6/9/19 8:19 AM, Ola Fosheim Grøstad wrote:
> On Sunday, 9 June 2019 at 08:36:30 UTC, Patrick Schluter wrote:
>> No, by putting NaN in d you hav e a deterministic error. In C and C++
>> you will have undefined behaviour that will vary with compiler,
>> version, options, OS version, architecture, position of the moon, etc.
>> and sometimes undetectable bugs.
>
> I don't think it is undefined though… If something has an arbitrary
> value, you could still compute with it, if the algorithm takes that into
> account. Assuming that all bit-patterns provides a defined value (which
> is the case for IEEE floating point bit-patterns).
>
> Anyway, the obvious advantage with having structs default initialized to
> all-bits-zero is that you can have an allocator that clears bits in the
> background (bypassing caches so they are not polluted).
>
> Then you have no penalty when allocating an array of one million struct
> values. Which is very useful. Just allocate memory-chunks that are
> already set to zero-bits.
>
> You usually want an array of floating point values to be pre-initialized
> to zeros. You almost never want an array of floating point values being
> initialized to NaN.
>
Yes, and further I would suggest that non-zero-bit initializations
violate the principle of least surprise.
As someone posted upthread, it would be interesting to take a poll of
new users (or non users, or perhaps the D-curious) and ask what their
best guess is for each default value.
More information about the Digitalmars-d
mailing list