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 联系我们: