2014-03-19 128 views
2
#include <stdio.h> 
#include <stdlib.h> 

int main(void) { 


    long size = 10000000; 

    long i = 0; 

    while (i < size) { 
    printf("%d\n", i); 
    i++; 


} 

    return EXIT_SUCCESS; 
} 

/* 
++++++++++++++++++++++++++++++++++++++++++++++++++++++ 


but commenting out printf, i get no error - even traced in a debugger and jumping to breaks after the while loop, i am able to get i reach the 10 billion mark. 
just like below - 


++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
*/ 


#include <stdio.h> 
#include <stdlib.h> 

int main(void) { 


long size = 10000000; 

long i = 0; 

while (i < size) { 
    //printf("%d\n", i);  
    i++; 


} 

return EXIT_SUCCESS; 
} 

回答

3

您正在使用錯誤的說明符long它調用未定義行爲的SIGSEV故障。改爲使用%ld

+0

好的建議 - 但只是做了 - printf(「%ld \ n」,i); - 仍然我seg段... – knostika

+0

我知道這是一個非常簡單的事情 - 我只是困擾,因爲我正在達到的限制... – knostika

+0

順便說一句,我使用glibc ... – knostika

2

您應該使用%ldprintf a long值(%d代替int)。

對於printf()

參數指定要打印的數據。 如果任何參數不是相應轉換說明符所期望的類型,或者參數的格式少於格式所需的參數,則行爲未定義爲。如果有比要求的格式更多的參數,外來參數進行評估,並忽略

+0

謝謝 - herohuyongtao - 試過已經printf(「%ld \ n」,我),可悲的說我仍然得到一個錯誤...(SIGSEGV) – knostika

0

你的調試器應該能夠提供一個回溯,所以你可以看到的是在堆棧的頂部什麼功能的(即當前正在運行)段錯誤的時間。在gdb中,這將是bt commnd。

+0

你是對的丹 - 我需要查看程序堆棧並檢查在我的環境中導致段錯誤的原因... – knostika

相關問題