开发者

When should objects be dynamically or automatically instantiated?

开发者 https://www.devze.com 2023-02-04 00:48 出处:网络
For example, should I use: Foo* object = new Foo ; or should I just use Foo object ; If I messed up the code one eithe开发者_开发问答r of those, please correct. It is early and I am a beginner.

For example, should I use:

Foo* object = new Foo ; 

or should I just use

Foo object ;

If I messed up the code one eithe开发者_开发问答r of those, please correct. It is early and I am a beginner.


In the first case you allocate your Foo object in heap, while in the second it's allocated in stack.

Usually things are allocated in heap if:

  • Foo is so big that it wouldn't fit in the stack.

  • You don't know when to allocate your object (you only need to allocate it under some circumstances).

  • You need to extend the lifetime of your object also after the stack gets popped.

In most other cases (I could be forgetting some other common scenarios when it's useful to dynamically allocate your object) it's probably suggested to allocate your object in stack, since it will cost less at runtime and will be less error prone and easier to be handled.


operator new should be avoided wherever possible. It's wasteful in all areas - syntactically, semantically, and at run-time with space and time. If you have an object that can be allocated on the stack or statically in a class, do it. Dynamic allocation should only be used when you need exactly that - dynamism. If you don't need it to be dynamic, don't use dynamic allocation.


The default in C++ should be to use the objects themselves and not pointers (smart or otherwise) until there are specific reasons to do otherwise.

To get a default-constructed object, you must leave the parentheses off:

Foo object;

otherwise, the compiler will take it as the declaration of a function.


Automatic instantiation should be the norm since the object life-time is then determined by the scope, and destruction is automatic. Dynamic instantiation is necessary when the object is required to exist outside of the current scope.

Dynamic instantiation may also be necessary if the object is excessively large, and you have limited stack space. However for a single object this would be an unusual case, and often indicative of a design flaw.

Dynamic instantiation is more typically used for arrays of objects, when the resulting array is either too large for the stack or the size of the array is not known a priori.

The important thing to remember is that if you dynamically instantiate an object you are responsible for explicitly destroying it. Not doing so results in a memory leak.


Allocating objects with a new operator requires that you clean up after then with a delete operator. So on most cases, if it isn't necessary, I'd go with the static instantiation.

However, since the static instantiation will put the object on the stack, you might run into memory problems: the stack can only hold so much data. So sometimes it might be necessary instantiate the object dynamically.


The second usage should be:

Foo object;

The difference between the two is that the first is allocating the object on the heap, and the second is allocating the object on the stack. There are many differences, but here are a few:

  1. When you allocate an object on the heap, it will be alive until you explicitly delete it, whereas an object allocated on the stack is only alive until the function ends.

  2. Allocating an object on the heap is more expensive (in terms of performance) than allocating objects on the stack.

  3. You can generally allocate many more objects on the heap than on the stack.

  4. Objects allocated on the heap may be passed around between threads, whereas objects allocated on the stack cannot (that's not 100% correct, you can make your function stay alive while the other thread is working on your object, but that's pretty much useless and inefficient).

  5. Objects allocated on the stack will be allocated as soon as you enter the scope where they are declared. There are cases when you don't necessarily want them to always be allocated, which is when you might prefer to use a dynamic allocation on the heap.


In C++, pointers should be used only when it is really required for you to allocate memory on heap (at runtime). One of the purposes of introducing pointers in C was to be able to pass by address so that the object to be modified could be passed as parameter. In C++, you can achieve that by passing References to objects.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号