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:
myclass* p3 = new myclass
equivalent to myclass* p3 = new myclass()
?The new operator lets developers create an instance of a user-defined object type or of one of the built-in object types that has a constructor function.
The new operator is used to initialize the memory and return its address to the pointer variable ptr1 and ptr2. Then the values stored at memory positions pointed to by ptr1 and ptr2 are displayed. Finally the delete operator is used to free the memory.
The difference between the two is that operator new just allocates raw memory, nothing else. The new operator starts by using operator new to allocate memory, but then it invokes the constructor for the right type of object, so the result is a real live object created in that memory.
There's no new / delete expression in C. The closest equivalent are the malloc and free functions, if you ignore the constructors/destructors and type safety.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With