Reputation: 210402
When I write this code and compile with /W4
long r;
__try { r = 0; }
__finally { }
return r;
I get:
warning C4701: potentially uninitialized local variable 'r' used
Why does this happen?
Edit (in 2024): This only happens in 32-bit mode, not 64-bit mode.
Upvotes: 2
Views: 10352
Reputation: 999
Adding this as an answer as it is of more interest than in just a comment:
The error never appeared until a labeled-statement: was inserted before the variable. Remove the goto
& associated label and there is no warning.
This might have something to do with how namespace pathing is setup with a similar warning C4702 generated on a line number before the insertion of a goto
block. MVCE still to be generated if anyone is interested.
Upvotes: 1
Reputation: 321
The compiler can't be sure the code inside of the try block will successfully run. In this case it always will, but if there's additional code in the try block r = 0 may never execute. In that case r is uninitialized hence the error.
It's no different than if you said:
long r;
if(something) {
r = 0;
}
return r;
(where 'something' is pretty much anything other than a constant true value).
Upvotes: 3
Reputation: 14943
Because long r;
creates r
but it is not initialized; it is null.
So it warns you that the variable is not initialized. In certain cases it will cause Null Pointers.
Upvotes: 0