Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

new vs *new in C++

I do this:

MyClass myObject = *new MyClass();

But a lot of people say I should do this:

MyClass *myObject = new MyClass();

Is there a performance difference. Or a logical reason to use the second method at all? I just prefer to use the first method to get rid of the pointer confusions.

like image 268
Archie Mejia Avatar asked Jun 02 '13 05:06

Archie Mejia


People also ask

What is 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 is new () in C?

new() The new operator requests for the memory allocation in heap. If the sufficient memory is available, it initializes the memory to the pointer variable and returns its address.

What is difference between new () and malloc ()?

malloc(): It is a C library function that can also be used in C++, while the “new” operator is specific for C++ only. Both malloc() and new are used to allocate the memory dynamically in heap. But “new” does call the constructor of a class whereas “malloc()” does not.

What is the difference between new and operator new?

There's no difference between "new operator" and "operator new". Both refer to the same thing: the overloadable/replaceable operator new function that typically performs raw memory allocation for objects created by new-expressions.


3 Answers

The first example says: Allocate memory on the stack for an instance of MyObject, then allocate memory on the heap for another one, construct the one on the heap, copy its contents to the one on the stack, and then lose track of the heap pointer so it can't be freed.

The second one says: Allocate a pointer on the stack, allocate space for an instance of MyObject on the heap, construct it, and assign the pointer to its address so it can be freed later.

The second case uses less memory, is faster, and doesn't leak memory. The first case says "I don't understand C++".

like image 21
Lee Daniel Crocker Avatar answered Nov 16 '22 00:11

Lee Daniel Crocker


Both are not same!
First gives you a Undefined behavior[Ref 1:] or a memory leak while second doesn't if you call delete later.

MyClass myObject = *new MyClass();

Allocates a object of type MyClass on freestore and then copies that object to myObject. You lose the pointer to the dynanically allocated object and thus can never deallocate it.
If MyClass destructor has side effects and your program depends on those side effects then it gives you Undefined Behavior if not then what you have is a simple plain memory leak.

MyClass *myObject = new MyClass();

Allocates a object of type MyClass on freestore and the points myObject to the address where the dynamically allocated object is placed. You still have the pointer to the object, which you can deallocate by calling delete later on.


If your question is, what is the best way to do this,
The answer is to not use dynamically allocated object at all:

MyClass obj;

Good Read:

Why should C++ programmers minimize use of 'new'?


[Ref 1:]
C++11 Standard: 3.8.4:

A program may end the lifetime of any object by reusing the storage which the object occupies or by explicitly calling the destructor for an object of a class type with a non-trivial destructor. For an object of a class type with a non-trivial destructor, the program is not required to call the destructor explicitly before the storage which the object occupies is reused or released; however, if there is no explicit call to the destructor or if a delete-expression (5.3.5) is not used to release the storage, the destructor shall not be implicitly called and any program that depends on the side effects produced by the destructor has undefined behavior.

like image 160
Alok Save Avatar answered Nov 16 '22 01:11

Alok Save


The first is nonsense.

MyClass myObject = *new MyClass();

The part after the = allocates memory and creates a new MyClass object. The first part creates a new MyClass object by calling copy constructor with the RHS MyClass object. Then the memory allocated in the RHS leaks because you don't have a saved pointer to delete it with.

The above statement is the same as writing.

MyClass myObject;

Followed by

Leak memory equal to size of MyClass.

First of all decide whether you want an object on the stack or on the heap.

If you want an object on the stack, then do

MyClass myObject;

This creates a MyClass Object - it serves well for most purposes.

If you need an object on the heap, then do

MyClass *myObject = new MyClass();

The first way - allocating on the stack is more efficient. You do heap allocation for other reasons

  1. At compile time, you don't know how many objects you need to create.
  2. Use classes polymorphically.
like image 23
user93353 Avatar answered Nov 16 '22 00:11

user93353