Segmentation Fault on creating an array in C

独自空忆成欢 提交于 2019-12-03 15:41:35

A four megabyte stack is pretty big. The default size on Windows is 1MB. You need to use the /STACK option to the linker to ask for a larger size.

Arrays created like that are stored on the stack. However, the stack has very limited size, therefore you are hitting a stackoverflow and a crash.

The way to do it is to allocate it on the heap:

int *a = (int*)malloc(MAX * sizeof(int));

//  Do what you need to do.

//  Free it when you're done using "a".
free(a);

Instead of malloc'ating memory you can specify your buffer as static. E.g.:

static int a[MAX];

Advantage of this approach is that you need not to track your memory allocation.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!