What is difference between instantiating an object using new vs. without

C++ConstructorNew Operator

C++ Problem Overview


In C++,

Aside from dynamic memory allocation, is there a functional difference between the following two lines of code:

Time t (12, 0, 0); //t is a Time object

Time* t = new Time(12, 0, 0);//t is a pointer to a dynamically allocated Time object

I am assuming of course that a Time(int, int, int) ctor has been defined. I also realize that in the second case t will need to be deleted as it was allocated on the heap. Is there any other difference?

C++ Solutions


Solution 1 - C++

The line:

Time t (12, 0, 0);

... allocates a variable of type Time in local scope, generally on the stack, which will be destroyed when its scope ends.

By contrast:

Time* t = new Time(12, 0, 0);

... allocates a block of memory by calling either ::operator new() or Time::operator new(), and subsequently calls Time::Time() with this set to an address within that memory block (and also returned as the result of new), which is then stored in t. As you know, this is generally done on the heap (by default) and requires that you delete it later in the program, while the pointer in t is generally stored on the stack.

N.B.: My use of generally here is speaking in terms of common implementations. The C++ standard does not distinguish stack and heap as a part of the machine, but rather in terms of their lifetime. Variables in local scope are said to have "automatic storage duration," and are thus destroyed at the end of local scope; and objects created with new are said to have "dynamic storage duration," and are destroyed only when deleted. In practical terms, this means that automatic variables are created and destroyed on the stack, and dynamic objects are stored on the heap, but this is not required by the language.

Solution 2 - C++

One more obvious difference is when accessing the variables and methods of t.

Time t (12, 0, 0);
t.GetTime();

Time* t = new Time(12, 0, 0);
t->GetTime();

Solution 3 - C++

As far as the constructor is concerned, the two forms are functionally identical: they'll just cause the constructor to be called on a newly allocated object instance. You already seem to have a good grasp on the differences in terms of allocation modes and object lifetimes.

Solution 4 - C++

I think you already understand all the differences. Assuming that you are well aware about the syntax difference of accessing a member of t through a pointer and through a variable (well, pointer is also a variable but I guess you understand what I mean). And assuming also that you know the difference of call by value and call by reference when passing t to a function. And I think you also understand what will happen if you assign t to another variable and make change through that other variable. The result will be different depending on whether t is pointer or not.

Solution 5 - C++

  • Use new: Call operator new function to get dynamic memory, and then to call the constuctor function.
  • Not use new: Will not call operator new function, just directly to call the constuctor function. The stack will be used directly, no use to malloc.

Solution 6 - C++

There is no functional difference to the object between allocating it on the stack and allocating it on the heap. Both will invoke the object's constructor.

Incidentally I recommend you use boost's shared_ptr or scoped_ptr which is also functionally equivalent when allocating on the heap (with the additional usefulness of scoped_ptr constraining you from copying non-copyable pointers):

scoped_ptr<Time> t(new Time(12, 0, 0));

Solution 7 - C++

There is no other difference to what you know already.

Assuming your code is using the service of default operator new.

Solution 8 - C++

No.. There is no other difference..

Solution 9 - C++

void foo (Time t)
{
  t = Time(12, 0, 0);
}

void bar (Time* t)
{
  t = new Time(12, 0, 0);
}


int main(int argc, char *argv[])
{
  Time t;
  foo(t);//t is not (12,0,0),its value depends on your defined type Time's default constructor. 

  bar(&t);//t is (12,0,0)
  return 0;
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionmanzy704View Question on Stackoverflow
Solution 1 - C++greyfadeView Answer on Stackoverflow
Solution 2 - C++Manoj RView Answer on Stackoverflow
Solution 3 - C++Ates GoralView Answer on Stackoverflow
Solution 4 - C++taskinoorView Answer on Stackoverflow
Solution 5 - C++haibo cuView Answer on Stackoverflow
Solution 6 - C++Christopher HuntView Answer on Stackoverflow
Solution 7 - C++LearnerView Answer on Stackoverflow
Solution 8 - C++liaKView Answer on Stackoverflow
Solution 9 - C++fangzhzhView Answer on Stackoverflow