4

I know how arrays stored on the heap. But how are arrays stored on the stack? Is the complete array pushed to the stack?

Armen Michaeli
  • 8,625
  • 8
  • 58
  • 95
Sebi2020
  • 1,966
  • 1
  • 23
  • 40

2 Answers2

4

Arrays are stored the same no matter where they are. It doesn't matter if they are declared as local variables, global variables, or allocated dynamically off the heap. The only thing that differs is where they are stored.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
2

Yes, the whole array is pushed on stack.

See the following answer on how variables are allocated on the stack:

https://stackoverflow.com/a/18479996/1814023

Armen Michaeli
  • 8,625
  • 8
  • 58
  • 95
  • 1
    so if I create an local array with 100.000 elements, this can cause an stack overflow? – Sebi2020 Nov 20 '13 at 11:22
  • @NishithJainMR: size of stack and heap are decided by the compiler on Windoze (i.e. at compile-time). Default on MSVC is 1Mb for each. Point is though, it is not defined in the C standard. – cdarke Nov 20 '13 at 11:42
  • (strictly it's the linker rather than the compiler, and only for the main thread, for other threads you can specify the stack size on creation https://learn.microsoft.com/en-us/cpp/build/reference/linker-options?view=msvc-160 ) – Pete Kirkham Jun 15 '21 at 13:23