1

I am trying to create a 2 dimensional int array of size 800.

This is my code:

#include <iostream>
int main()
{
    int array[800][800];
    std::cout << 1;
}

My problem is that it never prints 1. I am using g++ as my compiler so it might have something to do with it's inner workings.

Furthermore, I couldn't find anywhere on the web saying there was a limit (except for 2^32 and such) to the size of a c-style array. When I try creating one with size 700 it works just fine.

Does anyone have an idea where this limit comes into effect and how I can overcome it?

Herr Flick
  • 84
  • 3
  • 10

1 Answers1

3

Does anyone have an idea where this limit comes

It comes fromn the language implementation. The space available for automatic storage is typically limited to one or few megabytes (potentially less on embedded systems) and that space is shared with all automatic variables on the same thread of execution.

Since this area of memory is called the stack, the crash that you are experiencing is called a stack overflow.

how I can overcome it?

Use dynamic or static storage instead of automatic for large objects.

eerorika
  • 223,800
  • 12
  • 181
  • 301
  • I have 16 giga bytes ram on fedora 64 bits. How come it works fine for me? – AKL Sep 29 '20 at 09:52
  • @AKL type `ulimit -a` and check your stack size. It's probably big enough to handle the data. – Waqar Sep 29 '20 at 09:57
  • @AKL `How come it works fine for me?` the example array is about 2 megabytes. That fits within the default stack size of some language implementations, but not within others. Perhaps your system has a bigger stack. On the other hand, stack overflow is not guaranteed to crash your program either, unfortunately. – eerorika Sep 29 '20 at 09:58
  • @eerorika yes probably so – AKL Sep 29 '20 at 10:00