The largest size of 2-D array in C [duplicate]

孤人 提交于 2019-12-12 04:55:46

问题


in my C program, when I am taking an array like this

int a[100000][100000];

I am getting segmentation fault. Now if I am using array of size less than 1000 * 1000 , like this

int a[1000][1000];

I am not getting any problem.In my program I need to use a 10^5 * 10^5 array. What should I do to fix it .


回答1:


Dynamically allocate it with malloc. By declaring it statically you use the stack, which has a maximum size that the heap (used in dynamic allocations) does not.

int *pointer = malloc (sizeof (*pointer) * (100000*100000));

Then, to access it, use indices to represent the x and y coordinates.



来源:https://stackoverflow.com/questions/23472913/the-largest-size-of-2-d-array-in-c

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