12

I am using VS2010. My debug version works fine however my Release version kept on crashing. So In the release version mode I right clicked the project chose Debug and then chose start new instance. At this point I saw that an array that I had declared as such

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

Never gets initialized. Any suggestions on what might be going on.

TobiMcNamobi
  • 4,687
  • 3
  • 33
  • 52
MistyD
  • 16,373
  • 40
  • 138
  • 240
  • 2
    How did you check the array is not initialized? – Ivaylo Strandjev Jun 25 '13 at 14:34
  • By placing a breakpoint after it. I noticed that the array had no values in it. Also I tried doing something like this `int a = ma[0];` and no value shows up. – MistyD Jun 25 '13 at 14:39
  • 1
    Please read my answer below. You should not trust values displayed while debugging release build. Add a print statement to check if the values of the array are correctly set and I bet you will see they are. – Ivaylo Strandjev Jun 25 '13 at 14:40
  • 1
    @IvayloStrandjev But of course, adding the print may make his program work again. Additional uses of a variable will change the way it is handled by the optimizer. (It's possible that, depending on how he uses `ma`, that the optimizer does eliminate it entirely.) – James Kanze Jun 25 '13 at 14:45
  • @JamesKanze I am sorry but I do not understand what you mean by your comment – Ivaylo Strandjev Jun 25 '13 at 14:46
  • @IvayloStrandjev His problem is that the program crashes. Adding the print may mean that it no longer crashes. – James Kanze Jun 25 '13 at 15:12
  • @JamesKanze see the title. OP believes that the problem is that the array is not initialized. I am only pointing out that this is most probably not the problem. – Ivaylo Strandjev Jun 25 '13 at 15:15

2 Answers2

11

When you build in Release, the compiler performs many optimizations on your code. Many of the optimizations include replacing variables with hard-coded values, when it is possible and correct to do so. For example, if you have something like:

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

By the time the optimizer gets done with it, it will often look more like:

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

...and the variable n is eliminated from your program completely. If you are stepping through a release version of this program and trying to examine the value of n, you may see very odd values or the debugger may report that n doesn't exist at all.

There are many other optimizations that can be applied which make debugging an optimized program quite difficult. Placing a breakpoint after the initialization of an array could yield very misleading information if the array was eliminated, or if the initialization of it was moved to someplace else.

Another common optimization is to eliminate unused variables, such as with:

int a = ma[0];

If there is no code in your program which actually uses a, the compiler will see that a is unneeded, and optimize it away so that it no longer exists.

In order to see the values that ma has been initialized with, the simplest somewhat reliable approach is to use so-called sprintf debugging:

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

And see what is actually there.

John Dibling
  • 99,718
  • 31
  • 186
  • 324
8

If you debug release build the debugger will report bogus values or will not be able to display any values for most of your variables. The safest way to check that the value of a variable is in Release build is to use logging.

So most probably your array is initialized in Release just as in Debug build but you are not able to see that through the debugger. It seems you have some other problem that is causing the code to crash in Release. Look for some other uninitialized variable or some stack corruption/index out of bounds access.

Ivaylo Strandjev
  • 69,226
  • 18
  • 123
  • 176