Why are there so many different ways to use new operator in C++
I've just read the new operator explanation on the cplusplus.com. The page gives an example to demonstrate four different ways of using new operator as following:
// operator new example
#include <iostream>
#include <new>
using namespace std;
struct myclass {myclass() {cout <<"myclass constructed\n";}};
int main () {
int * p1 = new int;
// same as:
// int * p1 = (int*) operator new (sizeof(int));
int * p2 = new (nothrow) int;
// same as:
// int * p2 = (int*) operator new (sizeof(int),nothrow);
myclass * p3 = (myclass*) operator new (sizeof(myclass));
// (!) not the same as:
// myclass * p3 = new myclass;
// (constructor 开发者_运维技巧not called by function call, even for non-POD types)
new (p3) myclass; // calls constructor
// same as:
// operator new (sizeof(myclass),p3)
return 0;
}
My questions are:
- What is the best practice of using new operator?
- Is
myclass* p3 = new myclass
equivalent tomyclass* p3 = new myclass()
?
Because they have different purposes. If you didn't want new
to throw std::bad_alloc
on failure, you would use nothrow
. If you wanted to allocate your objects in existing storage, you would use placement new … if you want raw, uninitialized memory, you would invoke operator new
directly and cast the result to the target type.
The plain standard usage of new
in 99% of all cases is MyClass* c = new MyClass()
;
To your second question: the new Object()
vs. new Object
forms are not generally equal. See this question and the responses for the details. But that really is nitpicking. Usually they are equivalent, but to be on the safe side always pick new Object()
; Note that, in this particular sample they are equal because MyClass
doesn't have any members, so strictly speaking the answer to your question is yes.
1) Several of these are used only in specific, unusual situations. The plain old traditional one is best, most of the time anyway:
X * x = new X();
2) Yes, they are. If the constructor has no arguments, the parentheses are optional. If you're declaring an automatic variable -- i.e.,
X x;
then you must omit the parentheses, or it's a function declaration! As a result, many people will tell you to omit them in the new
expression as well. That's a good practice, I think, but I'm just used to including them.
Rather than "best practice", you need them in different situations. For example, most people just want to call new x()
to allocate some memory and create the object. However, sometimes you're in situations where you don't want an exception thrown in case new
fails, so you call new (nothrow)
instead to get a null back.
If you already have some memory allocated but need to create an object, you use "placement new" by calling new (p3) x
.
In the rare case where you need to use a class's memory allocator but not actually create an object, you use the (myclass*) operator new (sizeof(myclass))
syntax.
Here are my recommendations:
- Prefer not to use
new
; pass by reference when possible. (Avoids memory leaks). - Use
operator new
:x = new X(); // Remember to use delete
- Prefer
std::vector
to arraynew
:x = new X()[quantity]; // Remember to use delete []
- Don't use placement
new
unless you absolutely know the reasons.
In general, placement new is used to allocate from specific addresses or a special memory pool. This is an advanced topic.
精彩评论