下面的代碼編譯,但給分段故障時沒有使用GCC優化:GCC段錯誤,沒有優化
#include <stdio.h>
#define n 10000000
int main()
{
fprintf(stderr, "Array with size %ld\n", n * sizeof(double));
double a[n];
return 0;
}
$ gcc -O0 a.c && ./a.out
Segmentation fault (core dumped)
$ gcc -O1 a.c && ./a.out
Array with size 80000000
我-O1,-O2,-O3甚至與-oG它的工作原理進行測試。但用-O0它段錯誤。我正在使用GCC 5.3.0。
如果我刪除fprintf,或者如果我將數組更改爲靜態double [N],則沒有分段錯誤。
爲什麼?發生什麼事?
不相關,但常量的常用命名約定全是大寫,並且比單個字母更具描述性,例如:'ARRAY_SIZE'。 – szczurcio