floating point - nan initializers

Anders F Björklund afb at algonet.se
Sun Feb 19 23:45:32 PST 2006


Walter Bright wrote:

>>Like in Java: (jikes compiler)
>>*** Semantic Error: The variable "sum" may be accessed here before having 
>>been definitely assigned a value.
> 
> "may" be? That's why it isn't in D. Wishy-washy messages aren't a solution. 

Jikes is always so polite about it, other compilers are more terse:
* javac: "variable sum might not have been initialized"
* gcc: "warning: `sum' might be used uninitialized in this function"

But you are right, as it's leaving the possibility that it is wrong...
(it's an error in Java, and an optional -Wuninitialized warning in GCC)

> Furthermore, they encourage programmers to introduce dead assignments to get 
> rid of the message, leaving the mysterious assignment to confuse the 
> maintenance programmer trying to figure out what the code does.

Yes, sometimes workarounds like that are needed to "silence" GCC...

The warning also only occurs during optimization/register candidates:
http://gcc.gnu.org/onlinedocs/gcc-3.3.6/gcc/Warning-Options.html#index-Wuninitialized-214

There's also special attributes and preprocessor tricks to "help" it.

> Good, clean code should have every statement be reachable, and every 
> assignment mean something. Having the compiler force you to insert 
> meaningless assignments and unreachable code is just not helpful. 

You're only "forced" if you want it to compile without warnings, though.

Having it lintfree/warningless takes some initial effort, no doubt about
that. But after that it is usually "effortless", and helps catch bugs...

I know what you think about warnings :-), so it is probably not for D.
(Having Phobos compile with the -w flag takes same about of workarounds)


The only thing that has me confused, is the D overview says that D is 
for people who like using lint and compile with all warnings as errors.

And activities such as the above "not helpful" ones is what I associate
with those two extra code analysis passes. Maybe I'm stuck in the past ?

--anders



More information about the Digitalmars-d mailing list