C++ 堆栈内存与堆内存

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5836309/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 18:58:35  来源:igfitidea点击:

Stack Memory vs Heap Memory

c++memory

提问by Steveng

Possible Duplicate:
What and where are the stack and heap

可能的重复:
堆栈和堆是什么以及在哪里

I am programming in C++ and I am always wondering what exactly is stack memory vs heap memory. All I know is when I call new, I would get memory from heap. If if create local variables, I would get memory from stack. After some research on internet, the most common answer is stack memory is temporary and heap memory is permanent.

我正在用 C++ 编程,我总是想知道堆栈内存与堆内存到底是什么。我所知道的是,当我调用 new 时,我会从堆中获取内存。如果创建局部变量,我将从堆栈中获取内存。在互联网上进行了一些研究后,最常见的答案是堆栈内存是临时的,堆内存是永久的。

Is stack and heap memory model a concept of operating system or computer architecture? So some of it might not follow stack and heap memory model or all of them follow it?

堆栈和堆内存模型是操作系统还是计算机架构的概念?那么其中一些可能不遵循堆栈和堆内存模型,还是全部遵循它?

Stack and heap memory is the abstraction over the memory model of the virtual memory ( which might swap memory between disk and RAM). So both stack and heap memory physically might be RAM or the disk? Then what is the reason where heap allocation seems to be slower than the stack counterpart?

堆栈和堆内存是对虚拟内存(可能在磁盘和 RAM 之间交换内存)的内存模型的抽象。那么堆栈和堆内存在物理上可能是 RAM 还是磁盘?那么堆分配似乎比堆栈分配慢的原因是什么?

Also, the main program would be run in the stack or a heap?

另外,主程序会在堆栈中还是在堆中运行?

Also, what would happen if a process run out of the stack memory or heap memory allocated?

另外,如果进程用完分配的堆栈内存或堆内存会发生什么?

Thanks

谢谢

回答by Eoin

In C++ the stack memory is where local variables get stored/constructed. The stack is also used to hold parameters passed to functions.

在 C++ 中,堆栈内存是存储/构造局部变量的地方。堆栈还用于保存传递给函数的参数。

The stack is very much like the std::stack class: you push parameters onto it and then call a function. The function then knows that the parameters it expects can be found on the end of the stack. Likewise, the function can push locals onto the stack and pop them off it before returning from the function. (caveat - compiler optimizations and calling conventions all mean things aren't this simple)

堆栈与 std::stack 类非常相似:您将参数压入其中,然后调用一个函数。函数然后知道它期望的参数可以在堆栈的末尾找到。同样,该函数可以将局部变量压入堆栈并在从函数返回之前将它们从堆栈中弹出。(警告 - 编译器优化和调用约定都意味着事情没那么简单)

The stack is really best understood from a low level and I'd recommend Art of Assembly - Passing Parameters on the Stack. Rarely, if ever, would you consider any sort of manual stack manipulation from C++.

从低级别真正最好地理解堆栈,我建议使用Art of Assembly - Passing Parameters on the Stack。很少,如果有的话,您会考虑从 C++ 进行任何类型的手动堆栈操作。

Generally speaking, the stack is preferred as it is usually in the CPU cache, so operations involving objects stored on it tend to be faster. However the stack is a limited resource, and shouldn't be used for anything large. Running out of stack memory is called a Stack buffer overflow. It's a serious thing to encounter, but you really shouldn't come across one unless you have a crazy recursive function or something similar.

一般来说,堆栈是首选,因为它通常在 CPU 缓存中,因此涉及存储在其上的对象的操作往往更快。然而,堆栈是一种有限的资源,不应用于任何大型项目。耗尽堆栈内存称为堆栈缓冲区溢出。遇到这种情况很严重,但除非您有一个疯狂的递归函数或类似的东西,否则您真的不应该遇到它。

Heap memory is much as rskar says. In general, C++ objects allocated with new, or blocks of memory allocated with the likes of malloc end up on the heap. Heap memory almost always must be manually freed, though you should really use a smart pointer class or similar to avoid needing to remember to do so. Running out of heap memory can (will?) result in a std::bad_alloc.

堆内存就像 rskar 所说的那样。通常,使用 new 分配的 C++ 对象或使用 malloc 之类分配的内存块最终会在堆上。堆内存几乎总是必须手动释放,尽管您确实应该使用智能指针类或类似的类来避免需要记住这样做。耗尽堆内存可能(将?)导致 std::bad_alloc。

回答by rskar

Stack memory is specifically the range of memory that is accessible via the Stack register of the CPU. The Stack was used as a way to implement the "Jump-Subroutine"-"Return" code pattern in assembly language, and also as a means to implement hardware-level interrupt handling. For instance, during an interrupt, the Stack was used to store various CPU registers, including Status (which indicates the results of an operation) and Program Counter (where was the CPU in the program when the interrupt occurred).

堆栈存储器特指可通过 CPU 的堆栈寄存器访问的存储器范围。堆栈被用作在汇编语言中实现“跳转-子程序”-“返回”代码模式的一种方式,也用作实现硬件级中断处理的一种方式。例如,在中断期间,堆栈用于存储各种 CPU 寄存器,包括状态(指示操作的结果)和程序计数器(发生中断时程序中的 CPU)。

Stack memory is very much the consequence of usual CPU design. The speed of its allocation/deallocation is fast because it is strictly a last-in/first-out design. It is a simple matter of a move operation and a decrement/increment operation on the Stack register.

堆栈内存在很大程度上是通常 CPU 设计的结果。它的分配/解除分配速度很快,因为它是严格的后进/先出设计。这是对堆栈寄存器的移动操作和递减/递增操作的简单问题。

Heap memory was simply the memory that was left over after the program was loaded and the Stack memory was allocated. It may (or may not) include global variable space (it's a matter of convention).

堆内存只是程序加载和堆栈内存分配后剩下的内存。它可能(也可能不)包括全局变量空间(这是一个约定问题)。

Modern pre-emptive multitasking OS's with virtual memory and memory-mapped devices make the actual situation more complicated, but that's Stack vs Heap in a nutshell.

具有虚拟内存和内存映射设备的现代抢占式多任务操作系统使实际情况更加复杂,但简而言之就是堆栈与堆。

回答by rskar

It's a language abstraction - some languages have both, some one, some neither.

这是一种语言抽象——有些语言两者兼而有之,有些语言两者兼而有之,有些则两者都没有。

In the case of C++, the code is not run in either the stack or the heap. You can test what happens if you run out of heap memory by repeatingly calling newto allocate memory in a loop without calling deleteto free it it. But make a system backup before doing this.

在 C++ 的情况下,代码不在堆栈或堆中运行。您可以通过new在循环中重复调用分配内存而不调用delete释放它来测试堆内存不足时会发生什么。但在执行此操作之前进行系统备份