下面的代码在运行时产生段错误 11,我不明白为什么。有人可以提供一些见解吗?提前致谢。
#include <stdio.h>
int main() {
int s, e;
int i,j;
typedef struct coordinate {
int x;
int y;
} coordinate;
typedef struct cell {
int altitude;
coordinate lowest_neighbor;
int visited;
int basin;
} cell;
cell cells[1000][1000];
for (i = 0; i < 1000; i++){
for (j = 0; j < 1000; j++){
cells[i][j].altitude = 9;
}
}
printf("%d", cells[0][0].altitude);
return 0;
}
最佳答案
因为:
cell cells[1000][1000];
正在耗尽你的堆栈。假设结构中没有填充的四字节int
,该数组占用两千万字节,对于堆栈来说相当多。
作为快速修复(无论如何对于这个程序),请尝试:
static cell cells[1000][1000];
对于许多实现来说,这会将其从堆栈中取出。
这种方法通常有缺点,因为这意味着只有一个数组副本,而不是每个函数调用或线程一个,但这在这里并不重要,因为你的程序只创建反正就一次。
关于c - 为什么以下代码会导致段错误 11?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22651599/