4

If I have a class named Object, what's the difference between creating an instance just like that:

Object var;

and:

Object* var = new Object();

?

Bo Persson
  • 90,663
  • 31
  • 146
  • 203
Kactung
  • 690
  • 2
  • 8
  • 21

5 Answers5

17

Here you are creating var on the stack:

Object var;

So in the above, var is the actual object.


Here you are creating var on the heap (also called dynamic allocation):

Object* var = new Object()

When creating an object on the heap you must call delete on it when you're done using it. Also var is actually a pointer which holds the memory address of an object of type Object. At the memory address exists the actual object.


For more information: See my answer here on what and where are the stack and heap.

Community
  • 1
  • 1
Brian R. Bondy
  • 339,232
  • 124
  • 596
  • 636
11

This:

Object var();

Is a function declaration. It declares a function that takes no arguments and returns an Object. What you meant was:

Object var;

which, as others noted, creates a variable with automatic storage duration (a.k.a, on the stack).

James McNellis
  • 348,265
  • 75
  • 913
  • 977
  • I thought that in C++, you could call the default constructor by either mechanism? – franji1 Jun 16 '10 at 14:18
  • @franji1: It's a tedious and often frustrating intricacy of the C++ grammar. Sometimes the `var()` form can be used to explicitly value-initialize an object (e.g., in the initializer list of a constructor you can initialize a member variable this way). But, in this particular instance, you can't. `Object var;` will call the default constructor if there is one. If there isn't a user-declared constructor, you can perform value initialization by doing something like `Object var = Object();` or `Object var((Object()));` (extra parentheses are required). – James McNellis Jun 16 '10 at 14:34
  • What is `Object var(Object());` exactly? My guess is a function named var yielding an Object and taking an (anonymous) pointer to function that takes nothing and returns an Object? – fredoverflow Jun 16 '10 at 17:13
  • @Fred: That's exactly what it is. It's also C++'s [most vexing parse](http://www.informit.com/guides/content.aspx?g=cplusplus&seqNum=439). – James McNellis Jun 16 '10 at 17:15
5
Object var();

Declaration of a function that returns Object. To create an automatic object(i.e on the stack):

Object var; // You shouldn't write the parenthesis.

While:

Object* var = new Object();

Is a dynamically allocated Object.

Khaled Alshaya
  • 94,250
  • 39
  • 176
  • 234
3
Object var;

creates an Object that is valid until the end of the current scope, generally until the '}'

Object* varp = new Object();

creates a pointed to a newly created Object. The pointer (varp) is valid until the en d of the scope, but the object itself exists until delete is called on the pointer.

You should always prefer the first version, unless there is a reason to do the second. The second version takes more memory (you need space for the pointer as well as the object), takes more time (the system needs to allocate memory on the heap), and you need to make sure to call delete to free the memory when you are done with it.

KeithB
  • 16,577
  • 3
  • 41
  • 45
  • But if I always create objects into the stack, it's doesn't full too quickly? I thought it were better to create objects in the dynamic memory instead the stack :/ – Kactung Jun 16 '10 at 18:25
  • Its possible that this would be a problem if you have lots of objects, or they are very large. In practice, I've never run into it. – KeithB Jun 16 '10 at 20:10
1
Object var();

Allocates object on the stack, so you don't have to deallocate it

Object* var = new Object();

allocates object on the heap

Kiril Kirov
  • 37,467
  • 22
  • 115
  • 187
Max
  • 19,654
  • 13
  • 84
  • 122