www.digitalmars.com         C & C++   DMDScript  

D - Automatic initialization

reply Dario <Dario_member pathlink.com> writes:
Automatic initialization of variables can prevent some bugs,
but it leads to slower code if the compiler isn't smart enough
to understand where it is not necessary.

IMO there should be a way to tell the compiler not to initialize
a specific variable. For example:

noinit int a;
// declares an int and prevent it to be initialized to int.init

noinit struct S
{ int a, b; }
S s;
// s won't be automatically initialized
// (there can be a method Struct.init() that initializes it
// with constant and/or not-constant values.)
Sep 13 2003
parent "Walter" <walter digitalmars.com> writes:
"Dario" <Dario_member pathlink.com> wrote in message
news:bjvj92$1j17$1 digitaldaemon.com...
 Automatic initialization of variables can prevent some bugs,
 but it leads to slower code if the compiler isn't smart enough
 to understand where it is not necessary.

 IMO there should be a way to tell the compiler not to initialize
 a specific variable. For example:

 noinit int a;
 // declares an int and prevent it to be initialized to int.init

 noinit struct S
 { int a, b; }
 S s;
 // s won't be automatically initialized
 // (there can be a method Struct.init() that initializes it
 // with constant and/or not-constant values.)
I understand your concern, but the compiler can nearly always remove any redundant initializations, and the cases it can't do it on are uncommon and not worth worrying about. Those sorts of optimizations are called "dead assignment elimination".
Sep 13 2003