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 whic开发者_C百科h 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
You probably haven't initialised sum_difference
and sum_norm_1
to zero.
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);
精彩评论