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. :
cout << x;
I'm sure you'll see that x is 0.
EDIT:
Just to clear things out:
int main()
{
int x = 0;
x = 3;
return 0;
}
will translate to (in release mode, with full optimization):
00401000 xor eax,eax
00401002 ret
The variable not only isn't assigned, it doesn't even exist; as opposed to the debug version:
00411370 push ebp
00411371 mov ebp,esp
00411373 sub esp,0CCh
00411379 push ebx
0041137A push esi
0041137B push edi
0041137C lea edi,[ebp-0CCh]
00411382 mov ecx,33h
00411387 mov eax,0CCCCCCCCh
0041138C rep stos dword ptr es:[edi]
0041138E mov dword ptr [x],0
00411395 mov dword ptr [x],3
0041139C xor eax,eax
where the variable x is created and assigned twice (see mov dword ptr [x],0 and mov dword ptr [x],3 ).
If observable output is modified by the variable, i.e.:
int main()
{
int x = 0;
x = 3;
cout << x;
return 0;
}
the generated binary will look like this:
00401000 mov ecx,dword ptr [__imp_std::cout (40203Ch)]
00401006 push 3
00401008 call dword ptr [__imp_std::basic_ostream<char,std::char_traits<char>
0040100E xor eax,eax
see that even with this, x isn't actually generated, but rather the 3 value is put on the stack and printed directly.
But the observed behavior in this case is the same, since 3 is printed. To the user, whether the value of x, which should be 3, is printed, or 3 is printed directly, is irrelevant.
x is not even created.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With