简体   繁体   中英

Seg fault in case of large 2D array

I am writing a program to do some analysis on DNA sequences. Everything works fine except for this thing. I want to declare a 2D array of size m*n where m and n are read from an input file. Now the issue is that if m and n goes too large. As an example if m = 200 and n = 50000 then I get a seg fault at the line where I declare my array.

array[m][n];

Any ideas how to overcome this. I do need such an array as my entire logic depends on how to process this array.

Probably you are running out of stack space.
Can you not allocate the array dynamically on heap using malloc ?

You may want to have a look at this answer if you do not know how to do that.

As others have said it is not a good idea to allocate a large VLA (variable length array) on the stack. Allocate it with malloc :

double (*array)[n] = malloc(sizeof(double[m][n]));

and you have an object as before, that is that the compiler perfectly knows how to address individual elements array[i][j] and the allocation still gives you one consecutive blob in memory.

Just don't forget to do

free(array);

at the end of your scope.

Not sure what type you're using but for the following code I've assumed int.

Rather than doing this:

int array[200][50000];

Try doing this:

int** array = (int**)malloc(200);
for (int i = 0; i < 200; i++)
{
    array[i] = (int*)malloc(50000);
}

This will allocate "heap" memory rather than "stack" memory. You are asking for over 300mb (if you're using a 32bit type) so you probably don't have that much "stack" memory.

Make sure to cleanup after you're done with the array with:

for (int i = 0; i < 200; i++)
{
    free(array[i]);
}
free(array);

Feel free to use m and n instead of the constants I used above!

Edit: I originally wrote this in C++, and converted to C. I am a little more rusty with C memory allocation/deallocation, but I believe I got it right.

You are likely running out of stack space.

Windows for instance gives each thread 1MB stack. Assuming the array contains integers and you are creating it on the stack you are creating a 40MB stack variable.

You should instead dynamically allocate it on the heap.

The array (if local) is allocated in the stack. There is certain limits imposed on the stack size for a process/thread. If the stack is overgrown it will cause issues.

But you can allocate the array in heap using malloc . Typical heap size could be 4GB (this can be more or less depending on OS/Architecture). Check the return value of malloc to make sure that memory for the array is correctly allocated.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM