I have a windows form application built using VC++ dot net 3.
There is a integer variable say int x.
When I run the application in debug mode the value for x =0;
But when I run the application without debugging the value is huge 113901996.
The variable is initialzed as int x = 0
Why is this so? Some setting I need to check?
EDIT: As per your new info, that you’re forgetting to initialize the variable, the behavior is expected. You’re reading garbage memory.
Now, even if you did initialize the variable, you could still replicate this behavior by not using it in a manner that affects the observable behavior of the program. This is what my original answer treated – which I will leave here for future reference.
Are you actually using
x? If you’re just initializing it, the optimizer will most likely exclude the whole thing.If it’s not influencing the observable behavior of the program, the optimizer is free to cut anything from the binary.
The watch might also be lying to you, I’ve seen that happening in Release mode.
Can you post some minimal code that reproduces the problem?
If you test it with some output, i.e. :
I’m sure you’ll see that
xis0.EDIT:
Just to clear things out:
will translate to (in release mode, with full optimization):
The variable not only isn’t assigned, it doesn’t even exist; as opposed to the debug version:
where the variable x is created and assigned twice (see
mov dword ptr [x],0andmov dword ptr [x],3).If observable output is modified by the variable, i.e.:
the generated binary will look like this:
see that even with this,
xisn’t actually generated, but rather the3value is put on the stack and printed directly.But the observed behavior in this case is the same, since
3is printed. To the user, whether the value ofx, which should be3, is printed, or3is printed directly, is irrelevant.The bottom line is: if the behavior of the program is not changed by some statements, they are excluded. In your case, the variable
xis not even created.