3 ms·
I don't have a strong feeling either way, but sometimes not giving a value to a variable allows the compiler and static analyser to quite easily detect uses of
by minipci1321 10y ago
I don't have a strong feeling either way, but sometimes not giving a value to a variable allows the compiler and static analyser to quite easily detect uses of non-initialized. OTOH, assigning '0', a perfectly valid value, to a variable initially, might help to conceal bugs when the variable is not updated to the actual value before use.
We would really need a "not-a-value" for integral types in order to create them initialized by default, and there is no such thing unfortunately. This is different for pointers (except in very specific situations) and for floating-point types, so these should be initialized.
- nullc 10y agoThe compiler detecting uninitialized use catches a lot of real bugs. A compromise there would be to define it to be initialized to zero, but leave uninitialized use still defined as an error in the program (just not one optimizers can exploit.). Unfortunately the impact on optimization is also a consideration too.
- braveo 10y agoIn languages that allow you to do things like set the value of enumerations, I always start with 1. The reason being that an uninitialized variable, bad outside input, etc, is more likely to get caught as bad input rather than whatever happens to be in slot 0.