0

In C++, When I declare an array inside the function which was of size 10^7. I was unable to do so. But, when I declared this array with the same size globally, everything was running super fine. What I mean to say is=> Let say I declare the array in a function

void ArrayReturn(){
  int N = 1e7+10;
  int arr[N]={0};      //When I try to output the content of this array
                       // there is a blank screen only.
  // Now I start performing seive
}

But, at the same time when I declare arr globally, the output comes fine

int arr[10000010];
void ArrayReturn(){
  //perform sieve
  //output which uses the content of this array, comes fine now.
}
So, I just wanna know whether this issue is because something related to memory assigned to a function over stack is limited, or is there something else that I am missing or don't know at all? 
Kindly explain.

    
shihack
  • 57
  • 7
  • 2
    Yes. Allocate on the heap to work around it (using `std::vector`). – HolyBlackCat Feb 07 '21 at 12:17
  • Local variables are typically implemented using the process stack. The stack is limited in size, on Windows it's only a single MiB in size. – Some programmer dude Feb 07 '21 at 12:17
  • As a rule of thumb, I tend to limit the size of my buffers declared on the stack to *1 KB* or so. For bigger buffers, use heap. – prapin Feb 07 '21 at 13:12
  • Note that the C++ standard does not impose any limit, it is an implementation detail or a platform detail. The limit on Apple II is 256 bytes, on Amiga is 8 KB, on Macintosh is 8 MB, and on Windows is 1 MB. On some platforms, the value is fixed, but on others it can be overridden if specified. (How to specify it varies by platform.) – Eljay Feb 07 '21 at 13:38
  • 1
    Does this answer your question? [Why does a large local array crash my program, but a global one doesn't?](https://stackoverflow.com/questions/22945647/why-does-a-large-local-array-crash-my-program-but-a-global-one-doesnt) – phuclv Feb 07 '21 at 14:03
  • 1
    dupes: [C++ Difference between global and non-global arrays](https://stackoverflow.com/q/22492904/995714), [Why on declaring an array global in c++, the size that it can be given is larger than declaring it in main](https://stackoverflow.com/q/32409910/995714), [Array declaration : Global Vs Local](https://stackoverflow.com/q/31750293/995714), [How memory is allocated for a variable declared outside vs inside main()](https://stackoverflow.com/q/23381876/995714) – phuclv Feb 07 '21 at 14:08

3 Answers3

0

Local (e.g.; function scope) and global variables are typically stored on process (thread) stack. An array of your size (10^7 bytes, I assume) could not fit inside a typical stack, even if it was a sole thing there (which it isn't). Typical stack is up to 8 MiB in size, your array would be at least 9 MiB large.

Use dynamic allocation, e.g.; std::vector for encapsulated dynamic allocation, as std::array, or C-style arrays are allocated on stack.

KennnyCZ
  • 11
  • 3
0

It's because local variable that is declared in function will be allocated in stack memory space. Mostly stack of ordinary spec is 1MB or 8MB per process, which can't hold 10^7 number of integer or floating point.

On the contrary, global variable will be allocated in data segment. The limit of data segment size is large enough, and you could allocate 10^7 size array globally.

The more explanation about the location of each type of variables, you can refer to below url.

Where in memory are my variables stored in C?

jaylee
  • 196
  • 2
  • 8
0

Is it because of some stack size which limits the memory consumption??

Yes, the stack has a pretty small limit in most environments. Your 10^7 array is about 10 MiB in size times sizeof(element), which is bigger than most default stack sizes.

You could likely increase the limit by telling your operating system, your linker or your program loader, but that is a bad idea. Think about how you would calculate the limit. You need it to be bigger, but how big exactly? What if a function call another and then another? How many arrays do you have at a single point in time? Do functions recurse?

It is best that you use the heap for that. That means dynamically allocating memory. For a contiguous chunk of data, your best bet is to use std::vector.

Acorn
  • 24,970
  • 5
  • 40
  • 69