Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
553 views
in Technique[技术] by (71.8m points)

memory management - How do I choose heap allocation vs. stack allocation in C++?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...