使用堆变量还是堆栈变量更好? [英] Is it better to use heap or stack variables?

查看:29
本文介绍了使用堆变量还是堆栈变量更好?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一位有经验的 C++ 用户告诉我,我应该努力使用堆变量,即:

An experienced C++ user told me that I should strive for using heap variables, i.e.:

A* obj = new A("A");

相反:

A obj("A");

除了所有关于使用指针的好和灵活的东西之外,他说最好把东西放在堆上而不是堆栈上(堆栈比堆小?).这是真的吗?如果是为什么?

Aside from all that stuff about using pointers being nice and flexible, he said it's better to put things on the heap rather than the stack (something about the stack being smaller than the heap?). Is it true? If so why?

注意:我知道生命周期的问题.让我们假设我已经适当地管理了这些变量的生命周期.(即唯一需要关注的标准是没有生命周期问题的堆存储与堆栈存储)

NB: I know about issues with lifetime. Let's assume I have managed the lifetime of these variables appropriately. (i.e. the only criteria of concern is heap vs. stack storage with no lifetime concern)

推荐答案

根据上下文,我们可以考虑堆或栈.每个线程都有一个堆栈,线程通过调用函数来执行指令.当一个函数被调用时,函数变量被压入堆栈.当函数返回时,堆栈回滚并回收内存.现在线程本地堆栈有一个大小限制,它会有所不同并且可以在一定程度上进行调整.考虑到这一点,如果每个对象都是在堆栈上创建的,并且该对象需要大内存,那么堆栈空间就会耗尽,从而导致 stackoverflow 错误.除此之外,如果对象要被多个线程访问,那么将此类对象存储在堆栈中是没有意义的.

Depending on the context we can consider heap or stack. Every thread gets a stack and the thread executes instructions by invoking functions. When a function is called, the function variables are pushed to stack. And when the function returns the stack rollbacks and memory is reclaimed. Now there is a size limitation for the thread local stack, it varies and can be tweaked to some extent. Considering this if every object is created on stack and the object requires large memory, then the stack space will exhaust resulting to stackoverflow error. Besides this if the object is to be accessed by multiple threads then storing such object on stack makes no sense.

因此小变量,大小可以在编译时确定的小对象,指针应该存储在堆栈中.将对象存储在堆或空闲存储上的问题是,内存管理变得困难.有可能发生内存泄漏,这很糟糕.此外,如果应用程序尝试访问已删除的对象,则可能发生访问冲突,从而导致应用程序崩溃.

Thus small variables, small objects who's size can be determine at compile time and pointers should be stored on stack. The concern of storing objects on heap or free store is, memory management becomes difficult. There are chances of memory leak, which is bad. Also if application tries to access an object which is already deleted, then access violation can happen which can cause application crash.

C++11 引入了智能指针(共享的、唯一的),使堆的内存管理更容易.实际引用的对象在堆上,但由始终在堆栈上的智能指针封装.因此,当堆栈在函数返回事件或异常期间回滚时,智能指针的析构函数会删除堆上的实际对象.在共享指针的情况下,引用计数保持不变,当引用计数为零时删除实际对象.http://en.wikipedia.org/wiki/Smart_pointer

C++11 introduces smart pointers (shared, unique) to make memory management with heap easier. The actual referenced object is on heap but is encapsulation by the smart pointer which is always on the stack. Hence when the stack rollbacks during function return event or during exception the destructor of smart pointer deletes the actual object on heap. In case of shared pointer the reference count is maintained and the actually object is deleted when the reference count is zero. http://en.wikipedia.org/wiki/Smart_pointer

这篇关于使用堆变量还是堆栈变量更好?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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