C++ Difference between *new and new
This question already has an answer here:
These are not two separate things. There's not a new
and a *new
. The second is simply applying unary *
to the result of new Cat()
.
new Cat()
is a new-expression. It dynamically allocates and initialises a Cat
object and evaluates to a pointer to that object. If you then apply unary *
to that pointer, you dereference it to get the Cat
object.
There is not very often a good reason to apply unary *
immediately to a new-expression. The reason is because you're immediately following the pointer and haven't stored the pointer anywhere. Consider if you had done this:
Cat c = *new Cat();
This would result in a memory leak that you can't recover from. A Cat
object is dynamically allocated and then copied into c
. The dynamically allocated Cat
now lingers around and you don't have a pointer to it through which you can delete
the object. This is very bad.
Cat& c = *new Cat();
This is a little better, because at least now c
is just a reference to the dynamically allocated object. You can always do delete &c;
to destroy the object. However, it masks the fact that Cat
was dynamically allocated. If I were reading this code, I wouldn't expect c
to be referring to a dynamically allocated object.
You must remember to destroy dynamically allocated objects. Dereferencing the result of a new-expression makes that more difficult to achieve, so avoid it.
This expression:
new Cat();
Creates dynamically an object of type Cat
, and you are ignoring the returned pointer (not storing it, not dereferencing, nothing). This expression:
*new Cat();
Does the same as the one above, except that you are also dereferencing the pointer returned by new
. But dereferencing a pointer per se is an operation has no side-effect.
Concretely, therefore, the effect of both expressions is the same. The important bit is that you are leaking memory twice by losing the one and only reference to the objects you create dynamically.
Keep in mind that every object created with new
must be destroyed by a corresponding call to delete
. For instance, if you stored the returned pointer this way:
Cat* pCat = new Cat();
That would allow you to do, later on:
delete pCat;
And avoid memory leaks. Also, this:
Cat& cat = *new Cat();
Would allow you to do, later on:
delete &cat;
And avoid memory leaks again. Notice, however, that this would not be an option:
Cat cat = *new Cat();
The above would still give you a memory leak. The reason is that it would copy the object obtained by dereferencing the pointer returned by new
into cat
. In other words, cat
would be a different object than (although identical to) the one the new
expression created.
The object created by the new
expression, on the other hand, would be lost - resulting in a memory leak again.
In Modern C++, it is advisable to avoid manual memory management by calling new
and delete
; rather, consider using smart pointers (which one depends on the ownership policy that you need). For instance:
#include <memory>
// ...
std::shared_ptr<Cat> pCat = std::make_shared<Cat>();
Smart pointers take care of automatically destroying the referenced object when the last smart pointer to the pointed object is destroyed.
它有助于你吗?
Cat *ptr = new Cat();
Cat cat = *new Cat(); /* memory leak :( */
链接地址: http://www.djcxy.com/p/13802.html
下一篇: 新的和新的C ++区别