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?

N. Rak
  • 132
  • 2
  • 12
  • 3
    the limit is available memory. you are asking for 800x800 = 640000 `int`s on the stack. Thats quite something – 463035818_is_not_an_ai Sep 29 '20 at 09:41
  • If I do it on the heap, am I going to be in the clear? – N. Rak Sep 29 '20 at 09:42
  • 1
    different site, but it fits: https://softwareengineering.stackexchange.com/questions/310658/how-much-stack-usage-is-too-much – 463035818_is_not_an_ai Sep 29 '20 at 09:42
  • 1
    @HerrFlick Yea, heap is usually just limited by the real memory and even then there might be swapping to discs involved before you actually get `std::bad_alloc` exception. One exception is 32-bit application, which is limited to 4GB – Quimby Sep 29 '20 at 09:48
  • 1
    @Quimby Thanks! I'll use the heap instead (although originally I preferred the stack because I understood it's faster than heap allocation). – N. Rak Sep 29 '20 at 09:50
  • It works fine for me, even after initialization of each member. my setup fedora 64 bit 16 Giga Byte memory – AKL Sep 29 '20 at 09:51
  • @HerrFlick what is your setup? how much ram do you have on the system? is it embedded setup? – AKL Sep 29 '20 at 09:55
  • @dandan78 not really because I also want to understand how to solve it - I saw this post earlier and it mostly talks about super high limits (like size of `int`). – N. Rak Sep 29 '20 at 09:56
  • Common, on any modern device 640000 int is really small! – AKL Sep 29 '20 at 09:57
  • @AKL stack size != RAM size. – Waqar Sep 29 '20 at 09:58
  • @AKL I am using a Windows 64-bit with 16GB ram. – N. Rak Sep 29 '20 at 10:12

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
  • 232,697
  • 12
  • 197
  • 326
  • 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