I\'ve been using C++ for a short while, and I\'ve been wondering about the new keyword. Simply, should I be using it, or not?
1) With the new keywo
The short answer is: if you're a beginner in C++, you should never be using new
or delete
yourself.
Instead, you should use smart pointers such as std::unique_ptr
and std::make_unique
(or less often, std::shared_ptr
and std::make_shared
). That way, you don't have to worry nearly as much about memory leaks. And even if you're more advanced, best practice would usually be to encapsulate the custom way you're using new
and delete
into a small class (such as a custom smart pointer) that is dedicated just to object lifecycle issues.
Of course, behind the scenes, these smart pointers are still performing dynamic allocation and deallocation, so code using them would still have the associated runtime overhead. Other answers here have covered these issues, and how to make design decisions on when to use smart pointers versus just creating objects on the stack or incorporating them as direct members of an object, well enough that I won't repeat them. But my executive summary would be: don't use smart pointers or dynamic allocation until something forces you to.
Method 1 (using new
)
delete
your object later. (If you don't delete it, you could create a memory leak)delete
it. (i.e. you could return
an object that you created using new
) delete
d; and it should always be deleted, regardless of which control path is taken, or if exceptions are thrown.Method 2 (not using new
)
delete
it later.return
a pointer to an object on the stack)As far as which one to use; you choose the method that works best for you, given the above constraints.
Some easy cases:
delete
, (and the potential to cause memory leaks) you shouldn't use new
.new
Which method should I use?
This is almost never determined by your typing preferences but by the context. If you need to keep the object across a few stacks or if it's too heavy for the stack you allocate it on the free store. Also, since you are allocating an object, you are also responsible for releasing the memory. Lookup the delete
operator.
To ease the burden of using free-store management people have invented stuff like auto_ptr
and unique_ptr
. I strongly recommend you take a look at these. They might even be of help to your typing issues ;-)
The short answer is yes the "new" keyword is incredibly important as when you use it the object data is stored on the heap as opposed to the stack, which is most important!
Are you passing myClass out of a function, or expecting it to exist outside that function? As some others said, it is all about scope when you aren't allocating on the heap. When you leave the function, it goes away (eventually). One of the classic mistakes made by beginners is the attempt to create a local object of some class in a function and return it without allocating it on the heap. I can remember debugging this kind of thing back in my earlier days doing c++.
The second method creates the instance on the stack, along with such things as something declared int
and the list of parameters that are passed into the function.
The first method makes room for a pointer on the stack, which you've set to the location in memory where a new MyClass
has been allocated on the heap - or free store.
The first method also requires that you delete
what you create with new
, whereas in the second method, the class is automatically destructed and freed when it falls out of scope (the next closing brace, usually).