Why is char initialized to 0xFF ?
Patrick Schluter
Patrick.Schluter at bbox.fr
Sun Jun 9 08:36:30 UTC 2019
On Sunday, 9 June 2019 at 07:48:46 UTC, KnightMare wrote:
> On Saturday, 8 June 2019 at 18:04:46 UTC, Adam D. Ruppe wrote:
>> On Saturday, 8 June 2019 at 17:55:07 UTC, James Blachly wrote:
>>> char is a UTF8 character, but 0xFF is specifically
>>> forbidden[3] by the UTF8 specification.
>>
>> And that is exactly why it is the default: the idea here is to
>> make uninitialized variables obvious, because they will be a
>> predictable, but invalid value when they appear.
>
> double d;
> most compilers fire error "using unitialized variable".
Which is technically not possible in D because D always
initializes variables. In C and C++ if you'd declare
double d=0.0; you wouldn't get the "using unitialized variable"
warning either. Independantly if 0 is the right or the wrong init
value.
> another side "I(D compiler) will tell u nothing for that, but
> u'll get a shit! haha"
>
> ok. lets see structs now
> struct S { double d; }
> S s;
> in most compilers s will contains zeros. in C/C++ - garbage.
> men comes to D not as first language, they has troubles with
> garbage in structs already, and they still forget initialize it
> right (I do), so rule "all initialization is zeros" is the best
> and right thing that can be.
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.
> if u dont initialize use "= void" - is good too.
> but initialize ints as 0, ptrs as null, chars as #FF, doubles
> as NaN - is was invented under mushrooms
No. If there were an equivalent of NaN for ints it would also be
used ( Personnaly I really would prefer int.init == int.int_min
and uint.init == uint.uint_max).
Default initialisation of variable is here to have deterministic
behaviour between versions and runs, i.e. get rid of nasal
demons, not to mind read the appropriate initial value of a
variable, that is something the programmer still has the
responsibility for.
>
> men comes to D and see char=#ff,double=NaN
> https://www.youtube.com/watch?v=Qsa41csyNU8
More information about the Digitalmars-d
mailing list