0

I'm using a pretty simple 2D array to store values (it's part of a mandelbrot set program).

int toBeWritten[xres][yres]; // xres and yres are calculated based on command line arguments

The 2D array works fine until my numbers get larger.

These, for example, work:

int toBeWritten[1024][1160];

int toBeWritten[2048][2321];

But when the size of the array grows to this:

int toBeWritten[4092][4637]; // the size I start getting seg faults

int toBeWritten[8192][9284]; // the largest size I want to get to

I get a seg fault if I try and access this array at any point after creating it.

Is it simply too big? Am I not allocating memory correctly?

If I can't make a 2D array this large, how could I store the values instead?

Thanks for any help!

cdubbs85
  • 33
  • 1
  • 5
  • 2
    Is this array by any chance local to some function? – StoryTeller - Unslander Monica Dec 07 '17 at 09:52
  • 1
    Have you tried using `malloc` to allocate your arrays? Seems like using `malloc` is more effective in this case. – ionizer Dec 07 '17 at 09:54
  • Compilers usually put local variables (including arrays) on the stack, and the stack is limited. On Linux the default stack size is 8 MiB. On Windows it's only a single MiB. Assuming that `sizeof(int) == 4` (which is common today) then `int toBeWritten[4092][4637]` will be over 72 MiB (4092 * 4637 * 4 bytes)! – Some programmer dude Dec 07 '17 at 09:56

0 Answers0