1

I am a beginner in C++. I found two ways of object definition.

Demo d;

Demo *ptrD = new Demo();

Is there any difference between above two object definition in cpp

where Demo is a class

 class Demo 
    {
         // --------------
         //---------------
    };

Is there any difference in memory allocated to object created by these two statements?

erip
  • 16,374
  • 11
  • 66
  • 121
swj
  • 25
  • 7
  • 1
    It should be Demo *ptrd = new Demo(); – Sumeet Sep 02 '15 at 13:41
  • `new` allocates memory dynamically, while the first example does it statically. The rest: http://stackoverflow.com/questions/8385322/difference-between-static-memory-allocation-and-dynamic-memory-allocation – Maksim Solovjov Sep 02 '15 at 13:42
  • 1
    @Sumeet No, it shouldn't. There is no -need- to do this. Some people find this a good practice, but i personally disagree with that. – Nallath Sep 02 '15 at 13:47
  • 1
    @Nallath: The guy has written Demo d = new Demo();, during initial post. I posted so that he can correct it. – Sumeet Sep 02 '15 at 13:49
  • The `()` in `new Demo()` is an *initializer*, which in general case also creates extra differences between these two variants. If you want to restrict the question to matters of storage and lifetime, it would be a good idea to eliminate that difference first. Either `Demo d;` vs. `new Demo` or `Demo d{}` vs `new Demo{}`. – AnT stands with Russia Sep 02 '15 at 13:51

4 Answers4

1

of course. the first one is an object created and allocated from the stack. it will be deleted automatically when its scope is over. that has being said:

  1. you can't return it as pointer or reference - because then you'll return a memory address which does not point to a valid object
  2. if you return it, it will return as copy or move
  3. you don't have to be afraid of memory leaks, becuase it's de-allocated automatically
  4. allocating it is much faster then the alternative heap allocation

the second one is allocated from the heap. a heap is a huge memory block given to you by the OS. that has being said:

  • an object allocated with new will live on untill you call delete on it
  • it's scope is universal to the program
  • you must pass it as a pointer or reference (well, you should, anyway)
  • you must delete it on some point
  • heap allocation is slower
  • the dangers of memory leak is greater

there are many many other neuanses to these, including polimorphism, multi-threaded enviroments and much more. learn about memory managment, know it as the palm of you hand , but opt unique_ptr and shared_ptr when time comes by.

David Haim
  • 25,446
  • 3
  • 44
  • 78
1

For Demo d; d will be allocated on stack and automatically freed when the program leaves the current block.

For Demo *d = new Demo(); d is a pointer to an object allocated on heap. Such objects remain in existence until explicitly freed with delete operator: delete d.

Hrant
  • 496
  • 1
  • 5
  • 13
0

The difference is that writing Demo d; causes the object instance to be created and destroyed automatically (also known as RAII), whereas in the second case you have to worry about it.

Patryk W.
  • 82
  • 1
  • 5
0

In first case:

Demo d;

The object will get allocated on the stack and is automatically deleted once the scope of the declaration is left.

In second case:

Demo *d = new Demo();

The object will get allocated over free store/heap. You need to explicitly delete object by calling delete operator.

anderas
  • 5,744
  • 30
  • 49
Sumeet
  • 779
  • 6
  • 20