Maximum Stack Size for C/C+ Program?

后端 未结 4 1902
轻奢々
轻奢々 2020-12-01 12:42

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];
           


        
4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-01 13:07

    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 
    #include 
    #include 
    #include 
    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;
       }
    }
    

提交回复
热议问题