-2

I woud like to ask what is the difference between this

Tv *television = new Tv();

and

Tv television = Tv();
user1505497
  • 321
  • 5
  • 13
  • 1
    You don't need to do `Tv television = Tv();`, you can do `Tv television;` – quantum Sep 22 '12 at 15:55
  • Strongly suggest you read this - http://stackoverflow.com/questions/79923/what-and-where-are-the-stack-and-heap – Ben Cottrell Sep 22 '12 at 15:57
  • @xiaomao there is difference between `T i;` and `T i = T();` for all POD types. – PiotrNycz Sep 22 '12 at 16:09
  • 1
    To accept an answer, click the check mark next an answer (but only if that answer has helped). To find your other questions, click on your username, go to the **Questions** section, and click on a question you asked and do the same. – David G Sep 22 '12 at 16:10
  • @PiotrNycz didn't know about that. Can you explain what the difference is? – quantum Sep 22 '12 at 16:11
  • @xiaomao the construct `T t = T();` means that t is zero initialized, like `int i = int();` means that i is equal to zero, whilst `int i;` means that i contains garbage unless it is global, – PiotrNycz Sep 22 '12 at 16:23

1 Answers1

5

The first one creates a dynamically allocated Tv and binds it to a pointer to Tv. The duration of the Tv object is under your control: you decide when to destroy it by calling delete on it.

new Tv(); // creates dynamically allocated Tv and returns pointer to it
Tv* television; // creates a pointer to Tv that points to nothing useful
Tv* tv1 = new Tv(); // creates dynamicalls allocated Tv, pointer tv1 points to it.

delete tv1; // destroy the object and deallocate memory used by it.

The second one creates an automatically allocated Tv by copy initialization. The duration of the Tv object is automatic. It gets destroyed deterministically, according to the rules of the language, e.g. on exiting scope:

{
  // copy-initializaiton: RHS is value initialized temporary.
  Tv television = Tv(); 
} // television is destroyed here.

"exiting scope" may also refer to the end of the life of an object of a class that contains a Tv object:

struct Foo {
  Tv tv;
}

....
{
  Foo f;
} // f is destroyed, and f.tv with it.
juanchopanza
  • 223,364
  • 34
  • 402
  • 480
  • Dynamic, automatic: What's the difference? – David G Sep 22 '12 at 15:47
  • @David, dynamic objects have to be deleted with `delete`, automatic will automatically go out of the scope and get destroyed. – Rivasa Sep 22 '12 at 15:50
  • Dynamic objects are allocated from the heap; automatic objects are created on the stack. (Creating an object on the stack is a bit more efficient, since all the compiler has to do is increase the value of the stack pointer by sizeof(Tv) and then call the object's constructor; but the lifetime of an object on the stack is always equal to the lifetime of the function/method that declared it, which isn't always what you want) – Jeremy Friesner Sep 22 '12 at 15:58
  • @David I hope it is clear from my edits. Note that I avoided all mentions of "heap" and "stack" because the C++ standard makes no mention of those. But people often use the term "stack" for automatic allocation and "heap" for dynamic. – juanchopanza Sep 22 '12 at 16:06
  • @juanchopanza Your answered cleared up much confusion I had. Thanks. +1 – David G Sep 22 '12 at 16:08