How to debug a variable that is optimized in the Release assembly

I am using VS2010. My debug version works fine, however my Release version continues to fail. So, in release version mode, I right-clicked the project, selected "Debug", and then chose to start a new instance. At that moment, I saw that the array I called as such

int ma[4]= { 1,2,8,4}; 

It is never initialized. Any suggestions on what might happen.

+6
source share
3 answers

When you create Release, the compiler performs many optimizations in your code. Many of the optimizations include replacing variables with hard-coded values ​​when possible and correctly done. For example, if you have something like:

 int n = 42; cout << "The answer is: " << n; 

By the time the optimizer is done with this, it will often look bigger:

 cout << "The answer is: " << 42; 

... and the variable n completely excluded from your program. If you are releasing a version of this program and trying to check the value of n , you may see very odd values ​​or the debugger may report that n does not exist at all.

There are many other optimizations that make debugging an optimized program difficult. Placing a breakpoint after initializing an array can lead to very misleading information if the array was deleted, or if its initialization was moved to another location.

Another common optimization is to exclude unused variables, for example:

 int a = ma[0]; 

If your program does not have code that actually uses a , the compiler will see that a not required, and optimize it so that it no longer exists.

To see the values ​​that were initialized with ma , the simplest somewhat reliable approach is to use the so-called sprintf debugging:

 cout << "ma values: "; copy (ma, ma+4, ostream_iterator <int> (cout, ", ")); 

And look what really is.

+6
source

If you are debugging the release assembly, the debugger will report dummy values ​​or it will not be able to display any values ​​for most of your variables. The safest way to verify that the value of a variable is in Release build is to use logging.

So, most likely, your array is initialized in Release in the same way as in the Debug assembly, but you cannot see it through the debugger. You seem to have another issue that causes the code to crash in Release. Look at some other uninitialized variable or some access to the wrapper / index of the stack outside.

+4
source

An array is not a problem, something else is crashing. Without any other code, it is impossible to determine what the problem is :)

0
source

Source: https://habr.com/ru/post/948070/


All Articles