Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maximum Stack Size for C/C+ Program?

Tags:

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.

like image 715
pkthapa Avatar asked Dec 15 '14 10:12

pkthapa


People also ask

What is max size of stack in C?

In Visual Studio the default stack size is 1 MB i think, so with a recursion depth of 10,000 each stack frame can be at most ~100 bytes which should be sufficient for a DFS algorithm. Most compilers including Visual Studio let you specify the stack size.

What is the size of stack in C?

Stacks are temporary memory address spaces used to hold arguments and automatic variables over subprogram invocations. The default size of the main stack is about eight megabytes. Use the limit command to display the current main stack size as well as set it.

What is maximum stack size?

In Microsoft Windows 2000, if the Microsoft ASP.NET Worker Process (ASPNet_wp.exe) creates a thread, the maximum stack size of the thread is 1 MB. In Windows Server 2008 and higher, the maximum stack size of a thread running on 32-bit version of IIS is 256 KB, and on an x64 server is 512 KB.

How big is the stack size?

Stacks are temporary memory address spaces used to hold arguments and automatic variables during invocation of a subprogram or function reference. In general, the default main stack size is 8 megabytes.


2 Answers

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.

like image 67
eerorika Avatar answered Oct 01 '22 20:10

eerorika


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;    } } 
like image 29
Jagadeesh Nellimarla Avatar answered Oct 01 '22 19:10

Jagadeesh Nellimarla