堆栈内存与堆内存

发布于 2024-11-04 07:39:47 字数 530 浏览 1 评论 0原文

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

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

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

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

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

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

谢谢

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.

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?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

翻了热茶 2024-11-11 07:39:47

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

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

堆栈实际上最好从低级别理解,我建议 汇编艺术 - 在堆栈上传递参数。您很少(如果有的话)会考虑从 C++ 进行任何类型的手动堆栈操作。

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

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

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

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)

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

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.

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.

李不 2024-11-11 07:39:47

堆栈内存具体是指可通过 CPU 的堆栈寄存器访问的内存范围。堆栈被用作在汇编语言中实现“跳转子例程”-“返回”代码模式的一种方式,也是实现硬件级中断处理的一种手段。例如,在中断期间,堆栈用于存储各种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).

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

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.

浪推晚风 2024-11-11 07:39:47

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

对于 C++,代码既不在堆栈中运行,也不在堆中运行。您可以通过重复调用 new 在循环中分配内存而不调用 delete 来释放内存来测试如果堆内存耗尽会发生什么情况。 但在执行此操作之前请先进行系统备份

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 new to allocate memory in a loop without calling delete to free it it. But make a system backup before doing this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文