char[] initialization
Hasan Aljudy
hasan.aljudy at gmail.com
Sat Jul 29 09:05:18 PDT 2006
Robert Atkinson wrote:
> Hasan Aljudy wrote:
>
>>
>>
>> Derek wrote:
>>
>>> On Sat, 29 Jul 2006 06:29:21 -0600, Hasan Aljudy wrote:
>>>
>>>
>>>> kris wrote:
>>>>
>>>>> Andrew Fedoniouk wrote:
>>>>>
>>>>>
>>>>>> Could somebody shed light on the subject:
>>>>>>
>>>>>> According to http://digitalmars.com/d/type.html
>>>>>>
>>>>>> characters in D are getting initialized by following values
>>>>>>
>>>>>> char -> 0xFF
>>>>>> wchar -> 0xFFFF
>>>>>> dchar -> 0x0000FFFF
>>>>>>
>>>>>> what is the idea to have string initialized by valid character
>>>>>> code instead of 0?
>>>>>
>>>>>
>>>>> Try google?
>>>>>
>>>>> http://www.digitalmars.com/d/archives/digitalmars/D/3239.html
>>>>
>>>>
>>>> I don't understand why the compiler should initialize variables to
>>>> illegal values!!
>>>>
>>>> OK, is it because you have to initialize variables explicitly?
>>>> Just WHY?
>>>>
>>>> As far as I know, the notion that non-initialized variables are bad
>>>> is a side-effect of the C (and C++) language, because non-inited
>>>> variables are garbage.
>>>>
>>>> However, in D (and Java .. and others), vars are always initialized.
>>>> So, if the compiler can init variables to good defaults, why should
>>>> it still be considered a bad habit not to init variables explicitly?
>>>> That just makes no sense to me.
>>>
>>>
>>>
>>> I believe that D's philopsophy is that all datatypes are initialized to
>>> 'invalid' values if they possibly can be. The ones that can't are
>>> integers,
>>> bytes, and bools. References, floating point values, and characters are
>>> initialized to 'wrong' values.
>>>
>>
>> I know .. I was asking "but why?" :(
>
>
> The intent I believe is to signal the programmer as soon as possible
> showing they have missed something. In C/C++ an un-initialised variable
> can easily survive thousands of debug runs until it 'initialises' to a
> completely wrong value. Most often on a release build and a end-users
> system.
>
> Take floats. By starting at NaN, from the very start you'll know you
> missed initialising it. You'll catch the error earlier in your debug
> process.
Still missing my point.
in C/C++ that's a problem because un-initialized variables carry garbage.
in D, it's not; if you init them to a reasonable valid default, this
problem won't exist anymore.
If un-initializing is bad just for its own sake .. then the compiler
should detect it and issue an error/warning, otherwise it should default
to a reasonable valid value; in this case, zero for chars and floats.
More information about the Digitalmars-d
mailing list