2010-05-11 100 views
1

我每次使用'start without degugging'(ctrl-F5)而不是普通的'debug'时, (F5)。C运行'Debug'和'Start without debug'时变量的不同答案

当我试图找到norm_differnece在调试以下值(按F5)模式,它让我对norm_difference正确答案

normdifference = 1.000000

但在“开始不调试”(按CTRL-F5)错误的输出

normdifference = 1456816083547664100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.000000

以下是代码是使输出 注段:X [] =是存储DOUBLE的数组值

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); 

回答

2

可能您正在读取数组的末尾。调试模式下的一些编译器将启动内存但不处于释放模式,因此在调试时,错误读取获得0,而在发布时会得到一些大数字

或根据@Marcelo Cantos变量未初始化 - 在调试时它们可能会启动在0

+0

好吧,我去sum_difference初始化为零。 double sum_difference = 0; 同样的问题。 如何使用调试来检查它在何处,当它给我正确的结果时。一种困境。 – Craz 2010-05-11 10:02:58

+0

我们需要看到更多的代码 - 包括X的声明和n的值。理想的代码,我们可以编译并运行 – Mark 2010-05-11 10:04:28

+0

AH我看到你的意思是由于重写对不起,我已经定义了我的数组的参数错误的循环。 应该是 X [n + i] -X [i] 谢谢堆 – Craz 2010-05-11 10:06:44

1

您可能还没有初始化sum_differencesum_norm_1为零。

+0

好吧,我去并初始化sum_difference为零。 double sum_difference = 0; 同样的问题。 – Craz 2010-05-11 10:03:24

+0

'sum_norm_1'呢?对不起,我没有发现那一个。 – 2010-05-11 10:07:45

0

它看起来像你的代码中有一些错误。我猜它应该是:

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);