堆栈内存与堆内存 [英] Stack Memory vs Heap Memory

查看:168
本文介绍了堆栈内存与堆内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

堆栈和堆栈的内容和位置

我用C ++编程,我总是想知道什么是堆栈内存与堆内存。我所知道的是当我调用新的,我会从堆获得内存。如果创建局部变量,我会从堆栈获得内存。经过对互联网的一些研究,最常见的答案是堆栈内存是临时的,堆内存是永久的。

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.

堆栈和堆内存模型是操作系统或计算机体系结构的概念吗?所以一些可能不遵循堆栈和堆内存模型,或者所有这些模型都遵循它?

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?

堆栈和堆内存是虚拟内存的内存模型这可能会在磁盘和RAM之间交换内存)。因此,堆栈和堆内存物理上可能是RAM或磁盘?那么什么原因堆分配似乎比堆栈对手慢?

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?

此外,主程序将在堆栈或堆中运行?

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?

感谢

推荐答案

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

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设计的结果。其分配/解除分配的速度很快,因为它是严格的最后进/先出设计。这是一个简单的移动操作和堆栈寄存器上的递减/递增操作。

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.

堆内存只是程序加载后剩下的内存并分配了堆栈内存。

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

现代的先进的多任务操作系统与虚拟内存和内存映射设备使得它可以(或不可能)包含全局变量空间(这是一个惯例)实际情况比较复杂,但是Stack vs Heap简而言之。

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.

这篇关于堆栈内存与堆内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆