6

I have created a code block, like this.

proc()
{
    Z* z = new Z();
}

now the pointer declared inside method proc will have scope only till proc. I want to ask when the DTOR for z will be called automatically. whether when the controls come out of the method proc or when my application is closed.

Apoorva sahay
  • 1,640
  • 3
  • 23
  • 43

7 Answers7

17

The destructor will not be called at all. The memory used by *z will be leaked until the application is closed (at which point the operating system will reclaim all memory used by your process).

To avoid the leak, you have to call delete at some point or, better yet, use smart pointers.

NPE
  • 438,426
  • 93
  • 887
  • 970
12

This is a memory leak. What you probably should have is:

void
proc()
{
    Z z;
}

and skip the dynamic allocation. If an object's lifetime corresponds to its scope, you rarely need dynamic allocation.

If for some reason you do need dynamic allocation (e.g. because of polymorphism), then you should use some sort of smart pointer; std::auto_ptr works well here, and things like scoped_ptr, if you have them, may be even better.

James Kanze
  • 142,482
  • 15
  • 169
  • 310
  • Better use unique_ptr if the compiler support it, and your scoped_ptr should be something similar but is non-standard – Geoffroy Oct 04 '11 at 18:06
  • @Geoffroy `unique_ptr` is just an updated `auto_ptr`; if you know that you'll never have to support an older compiler, you can use it. `scoped_ptr` has far more limited semantics, since it doesn't support transfer of ownership. – James Kanze Oct 05 '11 at 07:39
7

This is one of the fundamentals in C++.

Dynamic allocation

In your case, memory allocation and consequent constructor call for Z will happen on new:

Z* z = new Z();

The opposite part for destruction and memory deallocation will happen on delete:

delete z;

But since your code don't have it, memory deallocation will never happen, plus you will lose the pointer z having no possibility deallocating the object in future. This is typical memory leak.

Declaration

On the other hand, if you declare object like this:

Z z;

Memory allocation and constructor will be called immediately right here at declaration point, and when object's scope of existence is finished (i.e. at the end of the function) destructor will be called automatically and memory will be deallocated.

Dynamic allocation vs Declaration

I will not go into debates about what is better and what is not, but rather will provide the excerpt from one of the articles that is linked below:

Unlike declarations, which load data onto the programs data segment, dynamic allocation creates new usable space on the programs STACK (an area of RAM specifically allocated to that program).

FYI : Stack = Performance, but not always the best solution.

References

For your pleasure : tic tac toe.

Community
  • 1
  • 1
Andrejs Cainikovs
  • 23,842
  • 2
  • 65
  • 84
4

You are going to have a memory leak unless you pass z to delete.

Ed Heal
  • 55,822
  • 16
  • 77
  • 115
  • In the code block in which i have declared *z, i am done with it, So could i call "delete z" just before return statement of the proc. – Apoorva sahay Oct 04 '11 at 10:43
  • 1
    @Apoorva : If you never need `*z` outside the block, you shouldn't use dynamic allocation. As James Kanze wrote, just use `Z z;` and the object will be destroyed for you when `proc()` returns. – MSalters Oct 04 '11 at 14:17
1

DTOR will not be called automatically. You should use "delete" keyword.

Artem Chilin
  • 392
  • 2
  • 11
1

The destructor of Z won't get called unless you put in a line like this in your code:

delete z;
Aamir
  • 13,616
  • 6
  • 40
  • 65
1

When you use new the object is allocated on the heap, the heap is shared between all your functions in your program i.e. you can say a bit loosely, that the scope for heap allocated objects is your program so without doing a delete on the object, it will exist until your program exits.

AndersK
  • 33,910
  • 6
  • 56
  • 81