1

when i try to create a 2d array of 512 by 512 i get an exception( Unhandled exception at 0x00A916D7 in ConsoleApplication3.exe: 0xC00000FD: Stack overflow (parameters: 0x00000000, 0x00752000).

int  main()
{

int a[512][512];
return 0;
}
bob
  • 41
  • 5
  • 4
    Stack overflow: <- very explicit error... Bob has $10 but wants to spend $50 .... – Mitch Wheat Sep 23 '13 at 00:26
  • possible duplicate of [Handling Huge Multidimensional Arrays in C++](http://stackoverflow.com/questions/4464670/handling-huge-multidimensional-arrays-in-c) – sashkello Sep 23 '13 at 00:28
  • possible duplicate of [C++ stack overflow - visual studio 2008](http://stackoverflow.com/questions/1583966/c-stack-overflow-visual-studio-2008) – EvilTeach Sep 23 '13 at 00:40

1 Answers1

3

Window's default stack size is 1MB, and the size of int (4 bytes) * 512 * 512 = 1MB.

When you declare an array inline, such as the line int a[512][512], this is done using the stack. Since there will be a few things already on the stack, that's why you're hitting a stack overflow (as Mitch pointed out) after "only" 508x508 (don't depend on that many!).

Instead of allocating the array of arrays on the stack, use new/malloc to allocate it on the heap. Depending on exactly what you're trying to do, arrays might not even be the right data structure.

Tawnos
  • 1,877
  • 1
  • 17
  • 26
Kitsune
  • 9,101
  • 2
  • 25
  • 24