2

I've made a 2D array on the stack by doing:

    grid gridArray[100][100] = {{}};

However, I get a stack overflow.

auto gridArray = new grid[100][100]();

If i put it on the heap, I don't get an error.

I don't exactly know why this is; is the stack unable to allocate as much memory as the heap? Is there any danger in the way I'm doing it now?

Thanks.

trincot
  • 317,000
  • 35
  • 244
  • 286
Anteara
  • 729
  • 3
  • 14
  • 33

1 Answers1

4

I don't exactly know why this is; is the stack unable to allocate as much memory as the heap?

That's exactly it. Stack space is limited. As a rule of thumb, if you have more than a few KB of data you should use the heap.

See: What and where are the stack and heap?

Community
  • 1
  • 1
John Kugelman
  • 349,597
  • 67
  • 533
  • 578