For malloc, I can already choose fairly easily to 0 out memory, but here it can get more expensive if one isn't careful.
For malloc, I can already choose fairly easily to 0 out memory, but here it can get more expensive if one isn't careful.
I've always wondered why the compiler can't warn about this at compile time. It should know when uninitialized stack space is being used since it is, after all, the compiler artificially designating what the stack is!
(Probably something to do with globals/scope, right?)
You could say "warn if there are any inputs, and any return values for any function I call, that can lead to a problem" but I suspect that would produce too many false positives.
int y, z;
if (x < 3) { y = 0; }
if (x < 4) { z = y + 1; }There are plenty of warnings in -Wall that are conservative. For example in g++ C++, out of order constructor initialization even for primitive types is warned for the same reason.
In this case the use of y on the RHS without explicit initialization should be warned.
It sounds annoying, but having a bug's behaviour change between different build modes is much more irritating.
The compiler could just be "conservative" like you said and warn whenever it couldn't statically determine validity, but note that initializing e.g. `y = 0` has a performance cost (cf. reordering constructor initializers).
void do_something(int *);
int f() {
int x;
do_something(&x);
return x;
}
Is the return of x a use of initialized memory? Maybe. Relink the program and the answer might change.I googled it and this[0] seemed relevant:
1655 — if it has arithmetic type, it is initialized to (positive or unsigned) zero;
If x was static, this would be true. But in this case, x is just a default automatic variable. For x to be static, it would have to be declared outside the function, or have the keyword 'static'.
For anyone else interested in a longer explanation these SO comments are pretty informative: http://stackoverflow.com/questions/14049777/why-are-global-v...
1652 If an object that has automatic storage duration is not initialized explicitly, its value is indeterminate.
(x is an 'auto' variable in the given program.)But the vastly more common case is uninitialized usage in the same compilation unit and I argue that that case should give a warning.