23

I've tried the below program. The intent by which this program was created is to discover more about stack sizes.

int main()
{
    int nStack[100000000];
    return 0;
}

After executing the above code, the program crashes due to huge stack size allocation. What is the maximum possible size of the stack? Is it fixed for every program/computer? Can it be increased?

I want to know for the sake of knowledge. If anyone can provide examples in C/C++, it would be very helpful.

Xenyal
  • 2,136
  • 2
  • 24
  • 51
pkthapa
  • 1,029
  • 1
  • 17
  • 27

6 Answers6

20

What is the maximum size of the stack?

Depends on implementation. One to few megabytes is typical on PC nowadays.

Is it fixed for every program/computer?

It's typically fixed on linking but standard does not define that it is. Some operating systems can limit the stack during runtime too (RLIMIT_STACK on linux for example).

Can it be increased?

It may be possible depending on implementation. See the documentation of your linker for details. And possibly the documentation for the OS and the executable format too.

You should allocate huge arrays like that dynamically.

eerorika
  • 232,697
  • 12
  • 197
  • 326
  • in that case, how can we know that how much amount of stack our program is gonna consume??? – pkthapa Dec 15 '14 at 10:28
  • @PKThapa In general, that's not possible. For programs that do not use recursion or variable-length arrays, you can usually assume that the stack will not grow too large. – fuz Dec 15 '14 at 10:37
  • Actually, it's typically fixed by the linker, not the compiler. – Martin James Dec 15 '14 at 12:04
  • Do managed languages like C# also have such a limitation on stack memory. I mean stack memory is always limited but is it so small for them too? I'm just wondering how CLR allocates an array of big size which is local to a function? Such an array would certainly go to stack memory area only. – RBT Feb 06 '17 at 00:31
  • 1
    @RBT yes, stack limitation applies to CLR as well. I don't know much about C#, but a little bit of googling shows that in C# all array variables are allocated on the heap (i.e. dynamically) unless you use `stackalloc` keyword. – eerorika Feb 06 '17 at 08:56
9

For Linux based applications, we can use getrlimit and setrlimit API's to know various kernel resource limits, like size of core file, cpu time, stack size, nice values, max. no. of processes etc. 'RLIMIT_STACK' is the resource name for stack defined in linux kernel. Below is simple program to retrieve stack size :

#include <iostream>
#include <sys/time.h>
#include <sys/resource.h>
#include <errno.h>
using namespace std;

int main()
{
   struct rlimit sl;
   int returnVal = getrlimit(RLIMIT_STACK, &sl);
   if (returnVal == -1)
   {
      cout << "Error. errno: " << errno << endl;
   }
   else if (returnVal == 0)
   {
      cout << "stackLimit soft - max : " << sl.rlim_cur << " - " << sl.rlim_max << endl;
   }
}
0

I tried below program.

int main()
{
    int nStack[519492];
    cout<<"Okay!";
    return 0;
}

The output:

Okay!

But if I increase the array size by 1 byte, the program crashes.

int main()
{
    int nStack[519493];
    cout<<"Okay!";
    return 0;
}

Output:

No output. Program crashes.
pkthapa
  • 1,029
  • 1
  • 17
  • 27
0

For this type, you can allocate the dynamic memory allocation for this and use Generic Representation of Stack for Memory allocation. If requiring a huge amount of memory, you can use a Linked List in the form of a Stack. For C/C++, Dynamic Memory allocation for Malloc/New. Hope you understand.

ouflak
  • 2,458
  • 10
  • 44
  • 49
0

Before the days of virtual memory, for example in V7 Unix, I don't believe there was a fixed limit for the stack. The stack grew downwards from the top of memory, and the heap grew upwards from the bottom (well, from the top of the text+data spaces, anyway), and as long as they didn't meet ("don't let the beams cross!" :-) ), you were fine.

What there was a limit on was how much the stack could grow at once. If you had a function that allocated, say a 1K local variable, you could call it recursively several dozen times before things went south. But if you had a function that allocated a 10K local variable, it tended to crash at once.

What was happening was that the OS was noticing whether your stack accesses exceeded the bounds of what had been allocated for the stack already. If you went past the end of the stack by a relatively small amount, the OS assumed that the stack was growing, and it allocated a little more for you. But if you tried to grow the stack by a lot all at once, what you got was something that looked like a stray pointer reference, midway between the top of the stack and the top of the heap, and you got a segmentation violation instead.

(This is from memory, so I could be wrong on the details. If I ever boot up the old PDP-11 again, I'll have to remember to test this out.)

Steve Summit
  • 45,437
  • 7
  • 70
  • 103
-1

To change the size of the stack allocated for the main thread that is raised by the process loader to run the code at it's entry point, look to your linker documentation. It's also possible to edit that stack size in the executable header metadata.

Martin James
  • 24,453
  • 3
  • 36
  • 60
  • 1
    It would be very helpful and informative if you share with us about how to edit the stack size like what you said above. I have no idea about this. – pkthapa Dec 16 '14 at 12:00