Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When and why to declare member variables on the heap C++

Ok, so I'm very new at C++ programming, and I've been looking around for a couple days for a decisive answer for this. WHEN should I declare member variables on the heap vs. the stack? Most of the answers that I've found have dealt with other issues, but I want to know when it is best to use the heap for member variables and why it is better to heap the members instead of stacking them.

like image 534
Ben Dixon Avatar asked Jul 13 '12 19:07

Ben Dixon


People also ask

When should you allocate on the heap?

If you need to control manually the lifetime of the object, allocate it on the heap. If the object is big and the stack is not big enough for it, allocate it on the heap.

Are member variables on the heap?

Member variables are members of the class itself. They are neither on the heap nor on the stack, or rather, they are where ever the class itself is.

When can you allocate memory in heap for the variable?

You can allocate memory at run time within the heap for the variable of a given type using a special operator in C++ which returns the address of the space allocated. This operator is called new operator.

What are heap variables in C?

The heap is a memory used by programming languages to store global variables. By default, all global variable are stored in heap memory space. It supports Dynamic memory allocation. The heap is not managed automatically for you and is not as tightly managed by the CPU. It is more like a free-floating region of memory.


2 Answers

There are two important concepts to grasp first:

  1. One should avoid thinking in terms of "heap" and "stack". Those are implementation details of your compiler/platform, not of the language.1 Instead, think in terms of object lifetimes: should the object's lifetime correspond to that of its "parent", or should it outlive it? If you need the latter, then you'll need to use new (directly or indirectly) to dynamically allocate an object.

  2. Member variables always have the same lifetime as their parent. The member variable may be a pointer, and the object it points to may well have an independent lifetime. But the pointed-to object is not a member variable.

However, there is no general answer to your question. Crudely speaking, don't dynamically allocate unless there is a good reason to. As I hinted above, these reasons usually correspond to situations where the lifetime needs to differ from its "parent".


1. Indeed, the C++ standard doesn't really talk about "heap" and "stack". They're important to consider when optimising or generally thinking about performance, but they're mostly irrelevant from a program-functionality point of view.
like image 112
Oliver Charlesworth Avatar answered Sep 22 '22 03:09

Oliver Charlesworth


Member variables are members of the class itself. They are neither on the heap nor on the stack, or rather, they are where ever the class itself is.

There are very few reasons to add a level of indirection, and allocate a member separately on the heap: polymorphism (if the type of the member is not always the same) is by far the most common.

like image 33
James Kanze Avatar answered Sep 19 '22 03:09

James Kanze