Possible Duplicate:
C programming, why does this large array declaration produce a segmentation fault?可能的重复:C编程,为什么这个大数组声明会产生分割错误?
I am reading an image in c language but i am unable to do so as my program is stopping in between... after debugging i found that it is due to array size... is there any restriction on maximum size of array? if i declare array of size 1400X1400 everything works fine but if i define array of size 1600X1400 my program stops working... why it is so... is there any limit imposed by compiler or OS on array size? and if so what is solution for this in c.
我正在阅读一个c语言的图片,但是我不能这么做,因为我的程序在中间停止了……调试之后,我发现这是由于数组的大小……阵列的最大尺寸是否有限制?如果我声明数组大小为1400X1400,一切正常,但是如果我定义数组大小为1600X1400,我的程序就停止工作了…为什么它是如此…编译器或操作系统对数组大小有什么限制吗?如果是,那么c的解是什么。
unsigned char idata[1400][1400]; //working fine
unsigned char idata[1600][1400]; //not working
4 个解决方案
#1
8
I am guessing that idata
is a local variable. The problem is that local variables are stored on the stack (technically "automatic storage"), and the stack is much smaller than the 6400 megabytes you're trying to allocate on it. Allocating that much storage on it causes a stack overflow.
我猜idata是一个局部变量。问题是本地变量存储在堆栈中(技术上是“自动存储”),并且堆栈比您试图在其上分配的6400兆字节要小得多。在其上分配这么多存储将导致堆栈溢出。
Try
试一试
unsigned char** idata = new unsigned char*[DIM1];
for (int i = 0; i < DIM1; ++i)
idata[i] = new unsigned char[DIM2];
// or
unsigned char (*idata)[DIM2] = new char[DIM1][DIM2];
To allocate it in the free store and you shouldn't have a problem.
把它分配到免费商店,你就不会有问题了。
EDIT:
编辑:
I just looked at the tags and didn't see you were only talking about C. If so, you can do the same thing but use malloc
instead of new
:
我只是看了一下标签,没有看到你只是在说c。如果是的话,你也可以用malloc代替new:
unsigned char** idata = malloc(sizeof(unsigned char*) * DIM1);
for (i = 0; i < DIM1; ++i)
idata[i] = malloc(DIM2);
// or
unsigned char (*idata)[DIM2] = malloc(DIM1 * DIM2);
And don't forget to free
(or delete[]
for C++) the memory you allocate to avoid memory leaks.
不要忘记为c++释放(或删除[])您分配的内存,以避免内存泄漏。
#2
2
If you declare this on stack (e.g. in some function) then yes, it will provide stack overflow.
如果您在堆栈上声明这个(例如在某些函数中),那么yes,它将提供堆栈溢出。
You can declare it as static ('global variable') or allocate memory dynamically.
您可以将它声明为静态(“全局变量”)或动态分配内存。
The Using malloc for allocation of multi-dimensional arrays with different row lengths question is pretty much about it.
使用malloc分配具有不同行长度的多维数组的问题是非常重要的。
#3
2
Here is how you allocate and free a 2D array on the free store in C:
以下是如何分配和释放一个2D数组在免费存储在C:
unsigned char (*idata)[1400] = malloc(1600 * 1400);
// ...
free(idata);
#4
0
When you use expressions like unsigned char idata[sz1][sz2]
, the space of the array is allocated in stack, while the space of the stack is somehow really small, which causes you problem.
当您使用像unsigned char idata[sz1][sz2]这样的表达式时,数组的空间被分配到堆栈中,而堆栈的空间非常小,这就导致了问题。
But if you use unsigned char* idata = new char*[sz]
, the space needed is allocated on the heap. Usually you can get the space you want.
但是如果使用无符号char* idata = new char*[sz],则需要在堆上分配所需的空间。通常你可以得到你想要的空间。
#1
8
I am guessing that idata
is a local variable. The problem is that local variables are stored on the stack (technically "automatic storage"), and the stack is much smaller than the 6400 megabytes you're trying to allocate on it. Allocating that much storage on it causes a stack overflow.
我猜idata是一个局部变量。问题是本地变量存储在堆栈中(技术上是“自动存储”),并且堆栈比您试图在其上分配的6400兆字节要小得多。在其上分配这么多存储将导致堆栈溢出。
Try
试一试
unsigned char** idata = new unsigned char*[DIM1];
for (int i = 0; i < DIM1; ++i)
idata[i] = new unsigned char[DIM2];
// or
unsigned char (*idata)[DIM2] = new char[DIM1][DIM2];
To allocate it in the free store and you shouldn't have a problem.
把它分配到免费商店,你就不会有问题了。
EDIT:
编辑:
I just looked at the tags and didn't see you were only talking about C. If so, you can do the same thing but use malloc
instead of new
:
我只是看了一下标签,没有看到你只是在说c。如果是的话,你也可以用malloc代替new:
unsigned char** idata = malloc(sizeof(unsigned char*) * DIM1);
for (i = 0; i < DIM1; ++i)
idata[i] = malloc(DIM2);
// or
unsigned char (*idata)[DIM2] = malloc(DIM1 * DIM2);
And don't forget to free
(or delete[]
for C++) the memory you allocate to avoid memory leaks.
不要忘记为c++释放(或删除[])您分配的内存,以避免内存泄漏。
#2
2
If you declare this on stack (e.g. in some function) then yes, it will provide stack overflow.
如果您在堆栈上声明这个(例如在某些函数中),那么yes,它将提供堆栈溢出。
You can declare it as static ('global variable') or allocate memory dynamically.
您可以将它声明为静态(“全局变量”)或动态分配内存。
The Using malloc for allocation of multi-dimensional arrays with different row lengths question is pretty much about it.
使用malloc分配具有不同行长度的多维数组的问题是非常重要的。
#3
2
Here is how you allocate and free a 2D array on the free store in C:
以下是如何分配和释放一个2D数组在免费存储在C:
unsigned char (*idata)[1400] = malloc(1600 * 1400);
// ...
free(idata);
#4
0
When you use expressions like unsigned char idata[sz1][sz2]
, the space of the array is allocated in stack, while the space of the stack is somehow really small, which causes you problem.
当您使用像unsigned char idata[sz1][sz2]这样的表达式时,数组的空间被分配到堆栈中,而堆栈的空间非常小,这就导致了问题。
But if you use unsigned char* idata = new char*[sz]
, the space needed is allocated on the heap. Usually you can get the space you want.
但是如果使用无符号char* idata = new char*[sz],则需要在堆上分配所需的空间。通常你可以得到你想要的空间。