Stack Exchange
C Different answers for a variable when running 'Debug' and 'Start without debug'
I keep getting this weird output from my code everytime I use the 'start without degugging' (ctrl-F5) as opposed to normal 'debug' (F5).
When I try to find the following value of norm_differnece in debug (pressing F5) mode, it gives me the correct answer for norm_difference 
normdifference = 1.000000
but in 'start without debugging' (pressing ctrl-f5) the wrong output
normdifference = 1456816083547664100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.000000
The following is a segment of code which is gives the output
Note: X[] = is a array of stored DOUBLE values
for(i=0;i<n;i++){
    sum_difference += (pow((X[i*n]-X[i]),2));
      }
    norm_difference = sqrt(norm_difference);
for(i=0;i<n;i++){
    sum_norm_1 += pow(X[i],2);
      }
    norm_1 = sqrt(norm_1);
  //Take square root of the sum of squares for the row
    printf("normdifference = %f \n norm_1 = %f \n",norm_difference,norm_1);


Possibly you are reading past the end of your array. Some compilers in debug mode will zeo memory but not in release mode so in debug the bad read gets 0 whilst in release it gets some large number
or as per @Marcelo Cantos your variables were not initialised - in debug they might start at 0

ok, i went and initialized sum_difference to zero.
double sum_difference = 0;
Same problem.
How can I use debug to check where it is overreading, when it gives me the correct result. Kind of a dilema.

We need to see more code - including the declaration of X and value of n. Ideally code that we can compile and run

AH I see what you mean by overreading sorry, i had defined the parameters of my array wrong for the loop.
Should have been
X[n+i]-X[i]
Thanks Heaps

yeah, I was about to post the whole code, but its huge since it does a dolittle LU decomposition and calls on multiple functions/files. Thank god, I spent the last hour trying to figure out what I did wrong.

You probably haven't initialised sum_difference and sum_norm_1 to zero.

ok, i went and initialized sum_difference to zero.
double sum_difference = 0;
Same problem.

What about sum_norm_1? Sorry, I didn't spot that one earlier.

It looks like you have a few bugs in your code. I'm guessing it should be:
sum_difference = 0; // <<< FIX
for (i = 0; i < n; i++)
{
    sum_difference += (pow((X[i * n] - X[i]), 2));
}
norm_difference = sqrt(sum_difference); // <<< FIX
sum_norm_1 = 0; // <<< FIX
for (i = 0; i < n; i++)
{
    sum_norm_1 += pow(X[i], 2);
}
norm_1 = sqrt(sum_norm_1); // <<< FIX
//Take square root of the sum of squares for the row
printf("normdifference = %f \n norm_1 = %f \n", norm_difference, norm_1);

