2

In C++ you can instantiate objects using the new keyword or otherwise...

Object o = new Object();

but you can also just do

Object o = Object();

what exactly is the difference between the two, and why would I use one over the other?

TylerH
  • 20,799
  • 66
  • 75
  • 101
kamikaze_pilot
  • 14,304
  • 35
  • 111
  • 171

3 Answers3

15

You can't do Object o = new Object(); The new operator returns a pointer to the type. It would have to be Object* o = new Object(); The Object instance will be on the heap.

Object o = Object() will create an Object instance on the stack. My C++ is rusty, but I believe even though this naively looks like an creation followed by an assignment, it will actually be done as just a constructor call.

QuantumMechanic
  • 13,795
  • 4
  • 45
  • 66
  • [The differences between heap and stack memory](http://stackoverflow.com/questions/408670/stack-static-and-heap-in-c) – mdec Apr 25 '11 at 04:58
  • `Object o;` would give you an object on the stack using the no-arg constructor. `Object o = Object();` does the same thing. Usually the second form is used to create an object on the stack when you are passing args to the constructor; `Object o = Object(x,y,x);` – Brian Roach Apr 25 '11 at 05:06
  • 2
    Technically the object need not be on the stack it depends on the context in which it is used (ie it may be a member of dynamically allocated object). Using the term stack/heap is very Java like and not really relevant to C++. The first version returns an a pointer to an object of `dynamic storage duration` the second creates a temporary object and assigns it to an object of `automatic storage duration`. – Martin York Apr 25 '11 at 07:42
  • My comment is to the above statement "stack / heap is very Java like and not really relevant to c++". In embedded systems, stack space is optimized for speed and memory efficiency but is smaller in size and typically used for std types. The heap is larger, slower and typically used for user defined types (UDT) – TheChrisONeil Jun 03 '14 at 01:47
3

The first type:

Object* o = new Object();

Will create a new object on the heap and assign the address to o. This only invokes the default constructor. You will have to manually release the memory associated with the object when done.

The second type:

Object o = Object();

Will create an object on the stack using the default constructor, then invoke the assignment constructor on o. Most compilers will eliminate the assignment call, but if you have (intended or otherwise) side-effects in the assignment operation, you should take that into account. The regular way to achieve creating a new object without invoking the assignment operation is:

Object o; // Calls default constructor
AK.
  • 743
  • 7
  • 13
1

I was Searching for a question like above but with variation, so I'll add my question here

I noticed difference in visual studio 2015 compiler and gcc v4.8.5.

class Object
{
public:
x=0;
void display(){ std::cout<<" value of x:   "<<x<<"\n";}
};

Object *o = new Object;
o->display(); // this gives garbage to this->x , uninit value in visualstudio compiler and //in linux gcc it inits this->x to 0

Object *o = new Object(); // works well
o->display();
PeerPandit
  • 309
  • 1
  • 4
  • 16