如何在C ++中选择堆分配与堆栈分配? [英] How do I choose heap allocation vs. stack allocation in C++?

查看:258
本文介绍了如何在C ++中选择堆分配与堆栈分配?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

其中一个C ++功能将它与其他语言区分开就是将复杂对象分配为成员变量或局部变量,而不是总是分配 new 。但是这会导致在任何给定情况下选择哪个问题。



有没有一些选择如何分配变量的标准?我应该在什么时候将成员变量声明为直接变量,而不是作为引用或指针?什么时候应该使用 new 分配变量,而不是使用分配在堆栈上的局部变量?

解决方案


其中一个C ++功能将它与其他语言区分开来


...是手动进行内存分配。但是让我们离开:




  • 当对象必须长时间运行时,即在一个对象必须超过一定范围时,是昂贵的或不可能复制或移动,

  • 当对象很大(其中如果你想在安全的一边,可能意味着几个千字节),以防止堆栈溢出,即使对象只是暂时需要的, 在堆上分配对象 pimpl(编译器防火墙)成语

  • 在堆上分配可变大小的数组,

  • <


请注意,在第二条规则中,通过大对象意味着像

  char buffer [1024 * 1024]; // 1MB buffer 

但不是

  std :: vector< char>缓冲区(1024 * 1024); 

因为第二个实际上是一个非常小的






  • <如果您需要堆分配,请使用指针

  • 如果您要共享结构,请使用指针

  • 使用指针或多边形引用, / li>
  • 如果您从客户端代码中获取对象并且客户端承诺保持活动状态,请使用引用,

  • 在大多数其他情况下使用值。 / li>


在适当情况下,建议使用智能指针。注意,在堆分配的情况下你可以使用引用,因为你总是可以 delete& ref ,但我不建议这样做。引用是伪装的指针,只有一个差异(引用不能为null),但它们也表示不同的意图。


One of the C++ features that sets it apart from other languages is the ability to allocate complex objects as member variables or local variables instead of always having to allocate them with new. But this then leads to the question of which to choose in any given situation.

Is there some good set of criteria for choosing how to allocate variables? When should I declare a member variable as a straight variable instead of as a reference or a pointer? When should I allocate a variable with new rather than use a local variable that's allocated on the stack?

解决方案

One of the C++ features that sets it apart from other languages

... is that you have to do memory allocation manually. But let's leave that aside:

  • allocate on the heap when an object has to be long-lived, i.e. must outlive a certain scope, and is expensive or impossible to copy or move,
  • allocate on the heap when an object is large (where large might mean several kilobytes if you want to be on the safe side) to prevent stack overflows, even if the object is only needed temporarily,
  • allocate on the heap if you're using the pimpl (compiler firewall) idiom,
  • allocate variable-sized arrays on the heap,
  • allocate on the stack otherwise because it's so much more convenient.

Note that in the second rule, by "large object" I mean something like

char buffer[1024 * 1024];  // 1MB buffer

but not

std::vector<char> buffer(1024 * 1024);

since the second is actually a very small object wrapping a pointer to a heap-allocated buffer.

As for pointer vs. value members:

  • use a pointer if you need heap allocation,
  • use a pointer if you're sharing structure,
  • use a pointer or reference for polymorphism,
  • use a reference if you get an object from client code and the client promises to keep it alive,
  • use a value in most other cases.

The use of smart pointers is of course recommended where appropriate. Note that you can use a reference in case of heap allocation because you can always delete &ref, but I wouldn't recommend doing that. References are pointers in disguise with only one difference (a reference can't be null), but they also signal a different intent.

这篇关于如何在C ++中选择堆分配与堆栈分配?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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