Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any limit on stack memory?

I was going through one of the threads. A program crashed because it had declared an array of 10^6 locally inside a function.

Reason being given was memory allocation failure on stack leads to crash.

when same array was declared globally, it worked well.(memory on heap saved it).

Now for the moment, let us suppose, stack grows downward and heap upwards.

We have:

---STACK---

-------------------

---HEAP----

Now , I believe that if there is failure in allocation on stack, it must fail on heap too.

So my question is: is there any limit on stack size? (crossing the limit caused the program to crash). Or am I missing something?

like image 516
Vikas Avatar asked May 06 '10 09:05

Vikas


People also ask

Is stack memory limited?

Disadvantages of using StackStack memory is very limited.

Can you run out of stack memory?

Yes. The stack is just a region of memory and as such is a finite resource and can run out if you abuse it.

Why is stack memory limited?

Because all threads in a process share the same address space, they have to divide it between them. And after the operating system has taken its part, there is "only" 2-3 GB left for an application. And that size is the limit for both the physical and the virtual memory, because there just aren't any more addresses.

What happens if stack memory is full?

Stack Overflow Error in Programming Due to that, whenever the stack memory gets completely filled, a stack overflow error occurs.


4 Answers

Yes, stack is always limited. In several languages/compilers you can set the requested size.

Usually default values (if not set manually) are about 1MB for current languages, which is enough unless you do something that usually isn't recommended (like you allocating huge arrays on the stack)

like image 199
Foxfire Avatar answered Oct 02 '22 08:10

Foxfire


Contrary to all answers so far, on Linux with GCC (and I guess it is true for all modern POSIX operating systems), maximum stack size is a safety limit enforced by the operating system, that can be easily lifted.

I crafted a small program that calls recursively a function until at least 10 GB is allocated on stack, waits for input on the terminal, and then safely returns from all recursive calls up to main.

#include <stdio.h>
#include <string.h>
#include <sys/time.h>
#include <sys/resource.h>

void grow(unsigned cur_size)
{
    if(cur_size * sizeof(int) < 10ul*1024ul*1024ul*1024ul) {
        unsigned v[1000];
        v[0] = cur_size;
        for(unsigned i = 1; i < 1000; ++i) {
            v[i] = v[i-1] + 1;
        }

        grow(cur_size + 1000);

        for(unsigned i = 0; i < 1000; ++i) {
            if(v[i] != cur_size + i)
                puts("Error!");
        }
    } else {
        putchar('#');
        getchar();
    }
}

int main()
{
    struct rlimit l;
    l.rlim_max = RLIM_INFINITY;
    l.rlim_cur = RLIM_INFINITY;
    setrlimit(RLIMIT_STACK, &l);

    grow(0);
    putchar('#');
    getchar();
}
like image 43
lvella Avatar answered Oct 02 '22 08:10

lvella


This all depends on what language and compiler you use. But programs compiled with for instance C or C++ allocate a fixed size stack at program startup. The size of the stack can usually be specified at compile time (on my particular compiler it default to 1 MB).

like image 21
Andreas Brinck Avatar answered Oct 02 '22 08:10

Andreas Brinck


You don't mention which programming language, but in Delphi the compile options include maximum and minimum stack size, and I believe similar parameters will exist for all compiled languages.

I've certainly had to increase the maximum myself occasionally.

like image 45
Cruachan Avatar answered Oct 02 '22 06:10

Cruachan