Search code examples
c++stack-overflowheap-memorystack-memory

2D array 100x100 stack overflow; no error on heap. c++


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.


Solution

  • 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?