When will the memory be released?

I created a block of code like this.

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

now the pointer declared inside the proc method will have scope only until proc. I want to ask when DTOR for z will be called automatically. no matter when the controls exit the proc method or when my application is closed.

+6
source share
7 answers

The destructor will not be called at all. The memory used by *z will leak until the application closes (after which the operating system will return all the memory used by your process).

To avoid leakage, you should call delete at some point or, better yet, use smart pointers.

+17
source

This is a memory leak. You probably need:

 void proc() { Z z; } 

and skip the dynamic allocation. If the lifetime of an object corresponds to its volume, you rarely need dynamic allocation.

If for some reason you need dynamic allocation (for example, due to polymorphism), then you should use some kind of smart pointer; std::auto_ptr works well here, and things like scoped_ptr , if you have them, might even be better.

+12
source

This is one of the basics in C ++.

Dynamic allocation

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

 Z* z = new Z(); 

The opposite part for destroying and freeing memory will happen on delete :

 delete z; 

But since your code does not have it, memory will never free, plus you lose the Z pointer, which does not have the ability to free an object in the future. This is a typical memory leak.

Declaration

On the other hand, if you declare an object as follows:

 Z z; 

The memory allocation and constructor will be called immediately right here at the point of declaration, and when the object of the existence of the object is completed (i.e. at the end of the function), the destructor will be called automatically and the memory will be freed.

Dynamic allocation and declaration

I will not enter into the debate about what is better and what is not, but rather will provide an excerpt from one of the articles, which is given below:

Unlike ads that load data into the program data segment, dynamic allocation creates a new usable space in STACK programs (the RAM area specially allocated for this program).

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

References

For your pleasure: tic tac sock .

+7
source

You will have a memory leak if you don't pass z to delete .

+4
source

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

+1
source

The Z destructor will not be called unless you enter a string in this code:

 delete z; 
+1
source

When you use a new object allocated on the heap, the heap is distributed between all your functions in your program, that is, you can say a little freely that the area for objects selected by the heap is your program, so without deleting the object, it will exist as long as your the program will not exit.

+1
source

Source: https://habr.com/ru/post/898578/


All Articles