"堆" vs“stack” [英] "heap" vs "stack"

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

问题描述

有人可以解释堆是什么以及堆栈是什么吗?为什么我应该关心?b $ b?我曾经知道,但后来我忘记了。而且我似乎无法在C ++常见问题解答中找到它。


我一直在阅读从堆中分配的速度是多么缓慢或者其他什么,我

不知道为什么会这样。


Joe

Can someone explain what a heap and what a stack is? And why I should
care? I used to know, but then I forgot. And I can''t seem to find it
in the C++ FAQ.

I keep reading how allocating from the heap is slow or something, and I
have no idea why that would be.

Joe

推荐答案

Joe Van Dyk写道:
Joe Van Dyk wrote:
有人可以解释什么是堆,什么是堆栈?为什么我应该关心?我曾经知道,但后来我忘记了。而且我似乎无法在C ++ FAQ中找到它。


A堆通常是一个相对无组织的数据存储库。 堆栈

是一个非常有序的数据容器,其元素排在第一个

- 最后一个中。方式。

我一直在阅读从堆中分配的速度有多慢,而且我不知道为什么会这样。
Can someone explain what a heap and what a stack is? And why I should
care? I used to know, but then I forgot. And I can''t seem to find it
in the C++ FAQ.
A "heap" is usually a relatively unorganized data repository. A "stack"
is a quite ordered data container with elements queued up in the "first in
-- last out" manner.
I keep reading how allocating from the heap is slow or something, and
I have no idea why that would be.




在比C ++标准使用的语言更宽松的语言中,堆是指是

免费商店和堆栈的同义词。是分配自动存储物品的地方

持续时间。


V

-

请在邮寄回复时从我的地址中删除资金



In more loose lingo than C++ Standard uses, "heap" is the synonym for the
free store, and "stack" is the place where objects with automatic storage
duration are allocated.

V
--
Please remove capital As from my address when replying by mail


Joe Van Dyk写道:
Joe Van Dyk wrote:
有人可以解释一下堆是多少什么是堆栈?为什么我应该关心?我曾经知道,但后来我忘记了。我似乎无法在C ++ FAQ中找到它。


堆栈是运行Last In First Out的数据结构。这是非常有效的,并且方便CPU通常在硬件中实现堆栈,因此

函数可以将其局部变量驻留在其上。当他们调用servant

函数时,将这些变量添加到当前堆栈的头部,然后将
指向其变量后的下一个位置。这就是为什么

函数可以调用函数,每个函数运行时都保存私有变量




堆只是堆栈的反面。您可以按任意顺序分配和解除分配任意大小的堆块。

我一直在阅读从堆中分配的速度有多慢,我
不知道为什么会这样。
Can someone explain what a heap and what a stack is? And why I should
care? I used to know, but then I forgot. And I can''t seem to find it in
the C++ FAQ.
A stack is a data structure that runs Last In First Out. That''s so efficient
and convenient that CPUs generally implement a stack in hardware, so
functions can park their local variables on it. When they call servant
functions, these add their variables to the current head of the stack, then
point the head to the next spot after their variables. So that''s how
functions can call functions, each keeping private variables for as long as
each function runs.

A heap is simply the opposite of a stack. You can allocate and de-allocate
arbitrarily sized chunks of heap, in an arbitrary order.
I keep reading how allocating from the heap is slow or something, and I
have no idea why that would be.




堆必须在分配和释放时执行更多的工作。它

搜索一个好的未使用区域,检查这个大于请求的

大小,在块周围写入标记来保留它,修复其他标记
堆中的
指向正确,并返回指向块的指针。当它从程序中取回块时,它会删除标记,并修复其他标记的




一个很好的替代方法是返回固定大小的块的自定义堆。这比'b
更快。如果每个块大于指针,则

堆可以将其空闲块列表直接存储到存储区域中。每个免费的

块包含指向下一个空闲块的指针。


-

Phlip
http://www.greencheese.org/ZeekLand < - 不是博客!!!



A heap must perform much more work at allocation and deallocation time. It
searches for a good unused region, checks this is larger than the requested
size, writes markers around the block to reserve it, fixes up other markers
in the heap to point correctly, and returns a pointer to the block. When it
gets the block back from the program, it erases the markers, and fixes up
the other markers.

A good alternative is a custom heap that returns fixed sized blocks. That''s
orders of magnitude faster. If each block is larger than a pointer, then the
heap can store its list of free blocks directly into storage area. Each free
block contains a pointer to the next free block.

--
Phlip
http://www.greencheese.org/ZeekLand <-- NOT a blog!!!


" Phlip"写道:
"Phlip" writes:
堆只是堆栈的反面。




很像蜥蜴就像一个蚂蚁的反面核反应堆?



Much like a lizard is simply the opposite of a nuclear reactor?


这篇关于&QUOT;堆&QUOT; vs“stack”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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