dwhitney67 |
05-27-2012 07:32 AM |
When you declared the 2d array on the stack, you most likely exceeded the limits imposed by your system. You can check the stack size limit by issuing the command 'ulimit -s'. On my system, it is set to 8192 Kbytes. Your array far exceeds this size (btw, I believe your array has a size of 65536 Kbytes).
If you want to allocate from the heap this large array, then use something like:
Code:
#include <stdlib.h>
...
const int ROWS = 4096;
const int COLS = 4096;
int** array = malloc(ROWS * sizeof(int*)); // allocate the rows
for (int r = 0; r < ROWS; ++r)
{
array[r] = malloc(COLS * sizeof(int)); // allocate the columns
}
...
array[10][50] = 10; // insert data at row 10, column 50
...
for (int r = 0; r < ROWS; ++r)
{
free(array[r]); // this frees the columns
}
free(array); // this frees the rows
An alternative is to declare a 1d array, and then programmatically manage access to the 1d array as if it were a 2d array; for example:
Code:
Code:
#include <stdlib.h>
...
const int ROWS = 4096
const int COLS = 4096;
int* array = malloc(ROWS * COLS * sizeof(int));
...
array[10 * ROWS + 50] = 10; // insert data at row 10, column 50
...
free(array); // frees allocated memory
|