5 ms·
In general it's impossible. Consider this C program: void do_something(int *); int f() { int x; do_something(&x); return x;
by gsg 10y ago
In general it's impossible. Consider this C program:
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.
- smhenderson 10y agoIsn't x already initialized to zero as it's static? I googled it and this[0] seemed relevant: 1655 — if it has arithmetic type, it is initialized to (positive or unsigned) zero; [0] http://c0x.coding-guidelines.com/6.7.8.html http://c0x.coding-guidelines.com/6.7.8.html
- nkurz 10y ago> Isn't x already initialized to zero as it's static? 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'.
- smhenderson 10y agoOK, thanks, appreciate the correction. I see what I had wrong now. For anyone else interested in a longer explanation these SO comments are pretty informative: http://stackoverflow.com/questions/14049777/why-are-global-variables-always-initialized-to-0-but-not-local-variables#14049791 http://stackoverflow.com/questions/14049777/why-are-global-v...
- gsg 10y agoWhat you want is just a few lines up: 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.)
- smhenderson 10y agoYeah, that's what I get for replying without making sure I'm correct! :-)
- CoolGuySteve 10y agoYou are correct that it is undecidable if do_something is from another object file (even then there can be some linker magic). But the vastly more common case is uninitialized usage in the same compilation unit and I argue that that case should give a warning.
- gliese1337 10y agoEven when proving an uninitialized read is undecidable, it is still possible to note that a particular bit of code contains a path which could lead to an uninitialized read, or that it is is impossible to prove that an uninitialized read doesn't happen, in which case a warning is still likely to be appropriate.