什么是更高效的栈内存或堆? [英] What is more efficient stack memory or heap?

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

问题描述


可能重复:


从内存分配的角度 - 堆栈内存或堆内存更有效率?它依赖于什么?



显然,动态分配与堆栈上的分配有一个开销。使用堆涉及找到可以分配存储器和维护结构的位置。在堆栈上它很简单,因为你已经知道在哪里放置元素。我想知道在最坏的情况下,在支持结构允许动态分配的开销是什么?

解决方案

通常更高效的速度和简单的实现!



我倾向于同意来自软件网站的Michael,他说: / p>


使用堆栈
更有效率。



当从堆中分配时,
堆管理器必须通过什么是
,有时是相对复杂的
过程,以找到
内存的空闲块。有时它必须看一下
,找到正确大小的



这通常不是可怕的金额
的开销,但它是肯定更多的
复杂的工作相比堆栈
的功能。当你从
栈中使用内存时,编译器能够立即从栈中取出一块内存
来使用。它是
从根本上来说是一个更简单的过程。



但是,堆栈的大小是
限制,所以你不应该使用
非常大的东西,如果你需要
大于
的东西,比如4k左右,那么你
应该总是从堆
中获取。



使用堆栈的另一个好处是
,它会在当前函数退出时自动清除
,你
不必担心关于清洁它
自己。你必须更多的
小心堆分配,使
确保他们被清理。使用
处理
的智能指针自动删除堆
分配可以帮助很多。



看到代码
做的东西喜欢从堆分配2整数
,因为程序员
需要一个指向2个整数和
的指针,当他们看到一个指针,他们只是
自动假设他们需要
使用堆。我倾向于看到这与
少经验的编码器有点 -
这是类型的事情,你
应该使用堆栈和只有
一个2整数的数组声明
堆栈。


引用 Joel on Software 网站上的一个非常好的讨论: / p>

堆栈与堆:效率更高?


Possible Duplicate:
C++ Which is faster: Stack allocation or Heap allocation

What is more efficient from memory allocation perspective - stack memory or heap memory? What it depends on?

Obviously there is an overhead of dynamic allocation versus allocation on the stack. Using heap involves finding a location where the memory can be allocated and maintaining structures. On the stack it is simple as you already know where to put the element. I would like to understand what is the overhead in worst case in milliseconds on supporting structures that allow for dynamic allocation?

解决方案

Stack is usually more efficient speed-wise, and simple to implement!

I tend to agree with Michael from Joel on Software site, who says,

It is more efficient to use the stack when it is possible.

When you allocate from the heap, the heap manager has to go through what is sometimes a relatively complex procedure, to find a free chunk of memory. Sometimes it has to look around a little bit to find something of the right size.

This is not normally a terrible amount of overhead, but it is definitely more complex work compared to how the stack functions. When you use memory from the stack, the compiler is able to immediately claim a chunk of memory from the stack to use. It's fundamentally a more simple procedure.

However, the size of the stack is limited, so you shouldn't use it for very large things, if you need something that is greater than something like 4k or so, then you should always grab that from the heap instead.

Another benefit of using the stack is that it is automatically cleaned up when the current function exits, you don't have to worry about cleaning it yourself. You have to be much more careful with heap allocations to make sure that they are cleaned up. Using smart pointers that handle automatically deleting heap allocations can help a lot with this.

I sort of hate it when I see code that does stuff like allocates 2 integers from the heap because the programmer needed a pointer to 2 integers and when they see a pointer they just automatically assume that they need to use the heap. I tend to see this with less experienced coders somewhat - this is the type of thing that you should use the stack for and just have an array of 2 integers declared on the stack.

Quoted from a really good discussion at Joel on Software site:

stack versus heap: more efficiency?

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

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