简体   繁体   English

什么限制了我在内存方面对堆栈的使用?

[英]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.

相关问题 用户定义的堆栈和内置堆栈在使用内存方面有什么区别? - What is the difference between user defined stack and built in stack in use of memory? 为什么我的自定义堆栈类使用了如此多的内存? - Why does my custom stack class use so much memory? 根据计算机内存和C ++来定义“字节”有什么不同? - what is the difference in defining the 'byte' in terms of computer memory and in terms of C++? 在内存使用或编译时间上哪个更好? - What is better in terms of memory usage or compile time? 为什么术语“自动”和“动态”优于C ++内存管理中的“堆栈”和“堆”这两个术语? - Why are the terms “automatic” and “dynamic” preferred over the terms “stack” and “heap” in C++ memory management? 堆栈 Memory 的起始地址是什么? - What is the Starting address of the stack Memory? 使用内存区域作为堆栈空间? - Use memory region as stack space? 在内存分配方面哪些更好 - 子对象或指向单独对象的指针? - What is better in terms of memory allocation - subobject or a pointer to a separate object? 是什么导致 memory 的未处理异常:堆栈溢出 - what is causing Unhandled exception at memory : Stack overflow 我的堆栈弹出实现是否泄漏内存? - Is my implementation of stack pop leaking memory?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM