Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

  1. What is the best practice of using new operator?
  2. Is myclass* p3 = new myclass equivalent to myclass* p3 = new myclass()?
like image 441
xiao 啸 Avatar asked May 13 '11 17:05

xiao 啸


People also ask

What is the main purpose of using the new operator?

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.

What does the new operator do in C?

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.

What is the difference between operator new and the new operator?

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.

Does C have a new operator?

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.


2 Answers

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.

like image 167
Alexander Gessler Avatar answered Oct 04 '22 20:10

Alexander Gessler


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.

like image 28
Ernest Friedman-Hill Avatar answered Oct 04 '22 20:10

Ernest Friedman-Hill