Pointer highlighting and normal declaration

Sometimes I see objects in different C ++ programs declared and used like this:

object *obj = new object; obj->action(); obj->moreAction(); //etc... 

Is there any benefit to this, instead of just doing:

 object obj; obj.action(); obj.moreAction(); //etc 
+4
source share
7 answers

Yes - you can save the pointer in the container or return it from the function, and the object will not be destroyed when the pointer goes out of scope. Pointers are used

  • to avoid unnecessarily copying an object,
  • to facilitate the creation of an optional object,
  • to control the lifetime of a user object,
  • to create complex graph-like structures,
  • for combinations indicated above.

This does not work for free - you need to destroy the object manually ( delete ) when you no longer need it, and deciding when this moment comes is not always easy, plus you can just forget to code it.

+5
source

The first form allocating objects on the heap gives you full control of (and full responsibility ) for the object’s live time: you must delete obj explicitly.

In the second form, the object is automatically and irrevocably destroyed when obj goes beyond the scope (when exiting the current code block).

+3
source

Another reason that no one mentioned.
The stack is usually 1 MB, so the creation of large objects should be performed on the heap (with a new one)

+2
source

Basically, you should only use β€œnew” if you want the object to be outside the validity period of the area in which you create it. For instance,:

X* g(int i) { /* ... */ return new X(i); } // the X outlives the call of g()

If you want the object to live only in scope, do not use "new", but simply define a variable:

 { ClassName x; // use x } 
+1
source

This is due to the fact that it has more control over the life cycle of the object in question (when using new ).

0
source

Yes, there is a good reason: you are much more likely to have the right program when using the latter form.

The problem is that the previous form (pointer) is C ism, in C++ you must use a smart pointer to ensure proper destruction of the object at the end of its life.

Now, if you use std::auto_ptr<Object> obj(new Object()); You have 3 advantages:

  • Now you control the life cycle of your object explicitly (but you cannot forget it).
  • you can save the object in a container with polymorphism
  • you do not clog the stack and therefore risk less
0
source

you can ask the opposite: when should you use the strange first option? basically, if you want to allocate a large object, because if you do not need to do this, and you can put it on the stack, this will be a much faster option: this is one of the main advantages of using C ++ over JAVA, which puts all objects on the heap. and this advantage is especially true when working with many, many selections of small objects: put them on the stack to increase speed. the overhead of posting a pointer to the difference. You can find here information about the extended pool library, which provides us with tools to manage such allocations.

0
source

Source: https://habr.com/ru/post/1301586/


All Articles