C/ c++程序的最大堆棧大小?

[英]Maximum Stack Size for C/C+ Program?


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.

我想為了知識而知道。如果有人能提供C/ c++的例子,那將非常有幫助。

4 个解决方案

#1


18  

What is the maximum size of the stack?

堆棧的最大大小是多少?

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

依賴於實現。現在PC上典型的是1到幾兆字節。

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).

它通常固定在鏈接上,但標准沒有定義它。一些操作系統也可以在運行時限制棧(例如linux上的RLIMIT_STACK)。

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.

你應該動態地分配巨大的數組。

#2


7  

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 :

對於基於Linux的應用程序,我們可以使用getrlimit和setrlimit API來了解各種內核資源限制,比如核心文件的大小、cpu時間、堆棧大小、良好的值,max。不。的過程等。“RLIMIT_STACK”是linux內核中定義的棧的資源名。下面是檢索堆棧大小的簡單程序:

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

#3


-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.

要更改進程加載程序為在其入口點運行代碼而引發的主線程分配的堆棧大小,請參閱鏈接器文檔。還可以在可執行頭元數據中編輯堆棧大小。

#4


-1  

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.

但是如果我將數組大小增加1字節,程序就會崩潰。

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

Output:

輸出:

No output. Program crashes.

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2014/12/15/9e843fb9ffa03a99f2197ada907a8543.html



 
  © 2014-2022 ITdaan.com 联系我们: