106

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?

Anycorn
  • 46,748
  • 41
  • 153
  • 250
manzy704
  • 1,061
  • 2
  • 8
  • 3
  • 10
    It doesn't really count as an answer, but aside the answers already given you may be interested to know that you can override operator new/delete if you want to write your own memory management (for better performance). –  Sep 09 '10 at 06:04

9 Answers9

129

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.

greyfade
  • 23,436
  • 7
  • 60
  • 78
  • "An address within that memory block" -- just any address anywhere within the block or a specific location such as the beginning? – Sev Sep 09 '10 at 08:27
  • 2
    @Sev: Generally close to the beginning. For POD classes, it'll be at or very near the beginning, and for classes with virtual members and/or inheritance, the pointer will generally be farther inside the block, to make room for the vtable and inherited members, if any. – greyfade Sep 09 '10 at 17:46
  • 2
    @greyfade: In the second line of code where t is a pointer to Time, I realize that t points to an object of type Time which is allocated on the heap, but is t itself allocated on the stack or the heap? – masotann Feb 08 '13 at 19:07
  • 2
    @Calpis: `t` is an "automatic variable" declared in local scope. So yes, it is typically allocated on the stack. But it's usually not relevant to the discussion, since it merely *points to* the `Time` object. – greyfade Feb 08 '13 at 19:13
  • 1
    @greyfade: Actually, it _is_ relevant, and it's nice to see someone ask about it. People forget that pointers-to-objects are objects themselves, and that they deserve our attention too! – Lightness Races in Orbit Oct 27 '13 at 12:17
  • @greyfade are both these created on the heap? or is `Time t (12, 0, 0);` created on stack? I was going through a similar [question](http://stackoverflow.com/a/6337327) and there was a discussion in the comments ([here](http://stackoverflow.com/questions/6337294/creating-an-object-with-or-without-new#comment33305066_6337327), [here](http://stackoverflow.com/questions/6337294/creating-an-object-with-or-without-new#comment37475660_6337327) and the rest of the comment thread), but I don't understand what he means. Could you clarify this. – yadav_vi Jan 26 '16 at 10:03
  • @yadav_vi: Is my answer not clear? `Time t` is created "generally on the stack" with automatic storage duration, whereas `new Time()` has dynamic storage duration and "generally [stores it] on the heap." "Automatic" in this sense means "controlled by the scope" and "dynamic" means "controlled by `new`/`delete`." – greyfade Jan 26 '16 at 17:38
  • @greyfade could you please give a hint about the "generally" statements in bold? What should I google to read about the exceptions to this general rule? – RoG Mar 26 '21 at 14:37
  • 1
    @RoG I've made an edit – greyfade Apr 01 '21 at 00:21
33

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();
BJ Myers
  • 6,073
  • 6
  • 33
  • 45
Manoj R
  • 3,073
  • 1
  • 19
  • 32
5

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.

Ates Goral
  • 126,894
  • 24
  • 129
  • 188
4

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.

taskinoor
  • 43,612
  • 11
  • 111
  • 136
2

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));
Christopher Hunt
  • 1,933
  • 1
  • 14
  • 19
2

There is no other difference to what you know already.

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

Learner
  • 587
  • 1
  • 5
  • 16
2
  • 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.
haibo cu
  • 94
  • 6
1

No.. There is no other difference..

liaK
  • 10,834
  • 10
  • 43
  • 70
-2
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;
}
fangzhzh
  • 1,655
  • 17
  • 24
  • I think your comment "//t is not (12,0,0),its value depends on your defined type Time's default constructor." applies to the line just above it, i.e. to the declaration of object t (Time t;). – AamodG Mar 08 '16 at 06:12