58

What is the difference between this:

Myclass *object = new Myclass();

and

Myclass object = new Myclass();

I have seen that a lot of C++ libraries like wxWidgets, OGRE etc use the first method... Why?

Robert S.
  • 25,266
  • 14
  • 84
  • 116
RaouL
  • 1,133
  • 4
  • 14
  • 17
  • 1
    The second line is valid without the `new` keyword.. `Myclass object = Myclass();`. Check [this question](https://stackoverflow.com/q/1764831/2300466) for more details. – Ahmad Ibrahim Jun 25 '18 at 08:54
  • 1
    The second one is wrong C++ code but valid Java code where all objects are represented as references. – MiCo Jan 30 '20 at 16:19

8 Answers8

82
Myclass *object = new Myclass(); //object has dynamic storage duration (usually is on the heap)
Myclass object; //object has automatic storage duration (usually is on the stack)

You create objects with dynamic storage duration (usually on the heap) if you plan on using them throughout a long period of time and you create objects with automatic storage duration (usually on the stack) for a short lifetime (or scope).

Joe Phillips
  • 49,743
  • 32
  • 103
  • 159
  • I just wanted to add question similiar to the above one: what is the difference between these two: Myclass *obj = new Myclass() ; and Myclass *obj = new Myclass; Kindly explain this coz I'm not able to find the difference. – Chandra Shekhar Jun 21 '18 at 09:59
  • 1
    @ChandraShekharRam There is no difference. They both call the default constructor – Joe Phillips Jun 21 '18 at 11:57
  • Thanks a lot for the explanation – Chandra Shekhar Jun 21 '18 at 12:04
  • Just to consolidate my understanding of class constructors, why can't we write `Myclass object();` ? – yuqli Jul 30 '18 at 12:27
  • I think you can – Joe Phillips Jul 30 '18 at 12:38
  • The main point is to show that the new keyword puts objects on the heap and creating an object without it will put it in the stack as a local var that gets cleaned up when the function returns. The usage of the constructor isn't really relevant to this answer – Joe Phillips Jul 30 '18 at 12:42
  • If you look at this question, it sort of explains it. One uses the copy constructor and one doesn't: https://stackoverflow.com/questions/11853700/c-object-instantiation-vs-assignment – Joe Phillips Jul 30 '18 at 13:51
  • 1
    Technically the difference between these two forms is not "heap" vs. "stack". The spec doesn't say where they have to be stored. The first has *dynamic storage duration*; the second has *automatic storage duration*. – ZachB Jan 13 '19 at 00:57
  • @JoePhillips better, thanks. Might clarify the explanatory text also since it still is heap vs. stack-oriented. – ZachB Jan 13 '19 at 02:11
57

The second is wrong !

You may use

MyClass object;

That will work.

Now, concerning how to choose between these two possibilities, it mainly depends on how long your object should live. See there for a thorough answer.

Community
  • 1
  • 1
Benoît
  • 16,798
  • 8
  • 46
  • 66
36

Your first line is 100% correct. Unfortunately, you can't create object with your second line in c++. There are two ways to make/create an object in c++.

First one is :

MyClass myclass; // if you only need to call the default constructor    
MyClass myclass(12); // if you need to call constructor with parameters*

Second one is :

MyClass *myclass = new MyClass();// if you only need to call the default constructor
MyClass *myclass = new MyClass(12);// if you need to call constructor with parameters

In c++ if you use the new keyword, object will be stored in heap. It's very useful if you are using this object for a long time period and if you use first method, it will be stored in stack. it can be used only short time period. Notice: if you use new keyword, remember it will return pointer value. You should declare name with *. If you use second method, it doesn't delete object in the heap. You must delete by yourself using delete keyword:

delete myclass;
AutonomousApps
  • 4,229
  • 4
  • 32
  • 42
Vishva Rodrigo
  • 569
  • 5
  • 9
  • 6
    -1: Technically it's not heap vs. stack, it's dynamic vs. automatic storage duration. It's also not about using for "a long time period" vs a "short" one. One can only be used while in scope, the other will never be automatically deleted. – ZachB Jan 13 '19 at 01:05
  • Same thing you are talking about i guess. Refer: https://craftofcoding.wordpress.com/2015/12/07/memory-in-c-the-stack-the-heap-and-static/ – Vishva Rodrigo Jan 14 '19 at 03:56
  • 1
    When a process needs memory, some room is created by moving the upper bound of the heap forward, using the brk() or sbrk() system calls. Because a system call is expensive in terms of CPU usage, a better strategy is to call brk() to grab a large chunk of memory and then split it as needed to get smaller chunks. This is exactly what malloc() does. It aggregates a lot of smaller malloc() requests into fewer large brk() calls. Refer: https://www.linuxjournal.com/article/6390 – Vishva Rodrigo Mar 25 '19 at 04:58
15

The new operator returns a pointer to the object it creates, so the expression Myclass object = new Myclass(); is invalid.

Other languages don't have explicit pointers like C++ so you can write statements like Myclass object = new Myclass();, but in C++ this is simply not possible. The return type of new Myclass(); is a pointer to a Myclass object, i.e. Myclass *.

Welbog
  • 59,154
  • 9
  • 110
  • 123
8

The first example creates a pointer to MyClass and initializes it to point to the result of the new operator.

The second will likely not compile, as it is trying to create a MyClass object and assign it to a MyClass pointer. This could work in the unlikely event that you have a MyClass constructor that accepts a MyClass pointer.

Fred Larson
  • 60,987
  • 18
  • 112
  • 174
6

Your first code line is correct while second code line is incorrect.

Myclass object=new Myclass();  //Incorrect code

Above code is incorrect as new Myclass(); return pointer to class and Myclass object; declares object of class and you are trying to assign pointer to class to the object of class, which is incorrect.

Your first code line is correct. But this declares pointer to class not the object of class.

Myclass *object = new Myclass();  //declares pointer to class.

To declare object of class you should write following code.

Myclass object;   //declares object of class Myclass

But you should note that the way of accessing class member using pointer to class and using object of class are different. following is code for accessing members of class.

pointer_to_class->member;  // accessing class member using pointer to class
object.member;             //accessing class member using object of class 
Deepak Gautam
  • 185
  • 4
  • 11
4

The first is correct.

The second will generally not compile. And if it does compile then the class is doing some complicated things in a constructor/assignment operator. And it's probably leaking memory.

Douglas Leeder
  • 52,368
  • 9
  • 94
  • 137
-1

Its posible Myclass name = Myclass();

Fire Code
  • 1
  • 1