[英]What limits my use of the stack in terms of memory?
In windows (or any other OS for that matter) what determines how much stack I can use? 在Windows(或任何其他操作系统)中,什么决定了我可以使用多少堆栈? The name of this very website makes me assume it's possible to run out of stack so should I avoid putting large amounts of data on the stack? 这个网站的名称让我觉得它可以用完堆栈,所以我应该避免在堆栈上放入大量数据吗?
在Win32上,默认堆栈大小为1MB,可以在调用CreateThread()和编译器设置时进行调整。
It is language specific, Compiler specific and probably OS specific, but you should put large amount of data on the heap and not on the stack. 它是特定于语言的,特定于编译器的,可能是特定于操作系统的,但是您应该将大量数据放在堆上而不是堆栈上。
Other SO post about this 关于此的其他SO帖子
There are ways to change the stack size - but I wouldn't mess with it ! 有办法改变堆栈大小 - 但我不会惹它!
If you want to know your stack size using trial and error - just create an array on the stack and see how much it lets you... 如果你想通过试验和错误知道你的堆栈大小 - 只需在堆栈上创建一个数组,看看它能让你多少...
Its completely OS specific and configurable, on linux you can check and change with the ulimit call in the shell. 它完全是OS特定的和可配置的,在linux上你可以检查和更改shell中的ulimit调用。
Depends on what you call large, my current Debians standard stack size is 8 megs, which is large enough to have large arrays of 1Meg for example. 取决于你所谓的大,我目前的Debian标准堆栈大小是8兆,这足够大,例如1Meg的大阵列。
You can set the stack size for your application in Visual Studio under 您可以在Visual Studio下为应用程序设置堆栈大小
Project->Properties->Linker->System
Although not recommended aa programing technique, its fairly simple to retrieve the amount of free stack space: 虽然不推荐使用aa编程技术,但检索可用堆栈空间量相当简单:
CONTEXT Context;
memset(&Context, 0, sizeof(Context));
RtlCaptureContext(&Context);
long stackFree = Context.Rsp;
On Windows, for a native C/C++ project in Visual Studio, the stacksize for the initial/main thread is set using the linker's /STACK
option ("Linker/System/Stack Reserve Size" in the IDE's project properties), and defaults to 1MB. 在Windows上,对于Visual Studio中的本机C / C ++项目,初始/主线程的stacksize是使用链接器的/STACK
选项(IDE的项目属性中的“链接器/系统/堆栈保留大小”)设置的,默认为1MB。 This is also the default thread stack size for new threads that don't specify something more specific. 这也是未指定更具体内容的新线程的默认线程堆栈大小。
For subsequently spawned threads, _beginthread()
, _beginthreadex()
and CreateThread()
all have a parameter to let you specify the stack size for a thread, which will default to what you set in the linker properties if you pass in zero. 对于随后生成的线程, _beginthread()
, _beginthreadex()
和CreateThread()
都有一个参数,可以让您指定线程的堆栈大小,如果传入零,则默认为您在链接器属性中设置的大小。
See http://msdn.microsoft.com/en-us/library/ms686774.aspx for details. 有关详细信息,请参阅http://msdn.microsoft.com/en-us/library/ms686774.aspx 。
操作系统将设置上限,但您可以使用链接器设置特定限制,通常具有特定默认值,通常远低于操作系统允许的限制。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.