为什么会有栈和堆呢?
为什么汇编语言同时使用栈和堆?它们看起来是多余的。
Why do assembly languages use both a stack and a heap? They seem redundant.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
为什么汇编语言同时使用栈和堆?它们看起来是多余的。
Why do assembly languages use both a stack and a heap? They seem redundant.
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(3)
它们并不多余。它们各自都有优点和缺点:如果使用得当,堆栈会更快,因为内存分配很简单(推入/弹出)。缺点是您只能添加和删除顶部的项目(因此得名“堆栈”)。另外,总的堆栈空间是有限的,当你用完时,你就会……好吧,堆栈溢出。相比之下,堆允许随机分配和释放,并且可以在其中存储大量数据,但缺点是分配会带来更多开销 - 对于每个分配的内存块,必须找到合适的空闲部分,并且在从长远来看,需要避免空闲空间的碎片,并且系统必须跟踪空闲块的位置。
您可以使用堆栈来传递小的短期值,例如本地计数器变量、函数参数、返回值等;这些适合推送/弹出分配方式。对于较大或长期存在的数据结构,可以使用堆。
They're not redundant. Each of them has strengths and weaknesses: A stack is faster if used right, because memory allocation is trivial (push / pop). The downside is that you can only add and remove items at the top (hence the name, stack). Also, total stack space is limited, and when you run out, you have a... well, stack overflow. The heap, by contrast, allows random allocation and deallocation, and you can store large amounts of data there, but the downside is that allocation carries more overhead - for each allocated block of memory, a suitable free portion must be found, and in the long run, fragmentation of the free space needs to be avoided, and the system must track where the free blocks are.
You use the stack to pass around small short-lived values, e.g. local counter variables, function arguments, return values, etc.; these lend themselves to push/pop allocation style. For larger or long-lived data structures, you use the heap.
您当然可以构建一个计算系统,将它们中的任何一个用作其唯一的内存模型。然而,它们都有相当不同的属性,各有优缺点。大多数系统都利用两者,以便从它们各自中获得好处。
堆栈
堆栈可以被认为是一堆盘子,你在盘子上写一个值并将其放在堆栈的顶部,这称为压入操作并将值存储在堆栈上。显然,您还可以从堆栈中移除顶板,这称为弹出操作。但新的分配必须始终位于堆栈的顶部。
堆栈往往用于局部变量和在函数之间传递值。一般来说,堆栈具有以下令人敬畏的属性:
堆栈的问题来自于事实:项目只能从堆栈顶部添加/删除。现在,当上下遍历函数调用时,这非常有意义:从堆栈中弹出函数输入,为堆栈上的局部变量分配空间,运行函数,从堆栈顶部清除局部变量并将返回值压入堆栈。另一方面,如果我想分配一些内存并将其传递给另一个线程,或者通常将其释放到远离分配位置的地方,那么我会遇到问题,当我想要时,堆栈不在正确的位置来释放内存。
您可以说堆栈有助于快速顺序内存分配。
堆
现在堆是不同的,每个分配通常是单独跟踪的。这会导致分配和释放的大量开销,但每一项都可以独立于其他内存分配进行处理,直到内存耗尽为止。
有许多算法可以实现这一点,在这里讨论它们可能有点不明智,但这里有一个链接,讨论了一些很好的简单堆分配算法:malloc 和 new 的替代品
因此堆有助于随机内存分配,但这需要运行时惩罚,但是这种惩罚通常比仅使用堆栈来处理这种情况所产生的惩罚要小。
You could certainly construct a computing system that utilised either one of them as its only memory model. However, they both have rather different properties each with its own good and bad points. Most systems utilise both so as to get the benefits from each of them.
Stacks
A stack can be thought of as a pile of plates, you write a value on a plate and put it on the top of the stack this is called a push operation and stores a value on the stack. You can obviously also remove the top plate from the stack this is called a pop operation. But new allocations must always be at the top of the stack.
The stack tend to be used for local variables and passing values between functions. Generally stacks have the following awesome properties:
The problem with the stack comes from the fact items can only be added/removed from the top of the stack. Now this makes great sense when traversing up and down through function calls: pop functions inputs from the stack, allocate space for local variables on the stack, run function, clear local variables from the top of the stack and push the return value onto the stack. If on the other hand I want to allocate some memory and say pass it to another thread or in general free it far away from where it was allocated all of a sudden I have a problem, the stack is not in the correct position when I want to free the memory.
You could say the stack facilitates fast sequential memory allocation.
Heap
Now the heap is different each allocation is generally tracked separately. This causes a lot of overhead for allocations and deallocations, but each one can be handled independently of other memory allocations, well until you run out of memory.
There are numerous algorithms for accomplishing this and it is probably a bit unwise to twitter on about them here but here is a link that talks about a few good simple heap allocation algorithms: Alternatives to malloc and new
So the heap facilitates random memory allocation but this comes with a runtime penalty, however that penalty is often small that what would be incurred if you had to handle the situation using just the stack.
它与内存处理和管理有关。
x86 架构有不同类型的寄存器。
x86 架构上有硬件支持内存管理的可能性等等。
堆栈供指令指针使用,堆在某些应用中用于数据段。
要了解更多信息,我建议您阅读以下链接:
It is about the memory handling and managing.
There are different type of registers of x86 architectures.
There are possibilities of hardware supported memory management on x86 architecture and so on.
Stack is used by instruction pointer, Heap is for data segment in some applications.
To read more I advice you read the following links: