Domanda

Why must we use destructors to de-allocate memory in c++,

As we can use

delete or delete[]   

Is it not true that all the memory used up by a program is released when the program terminates.

È stato utile?

Soluzione 2

"we use destructors to de-allocate memory"

What you are actually writing about are deallocation functions operator delete and operator delete[].

"Is it not true that all the memory used up by a program is released when the program terminates?"

AFAIK this is OS specific, yet the point is not what happens after the program terminates. The point is about what happens during the execution. There are many applications that run for several hours or even weeks and memory leaks might have very unpleasant consequences in these (not that memory leaks wouldn't be unpleasant in terms of other programs).

When you program reaches the point, where the resources that you've allocated are not needed anymore, you should do your very best to free them using appropriate means. And once your program terminates: Relying on OS cleaning your mess doesn't seem to be much of a good practice ;)

Altri suggerimenti

Very often, it is not enough to get the memory back only after your program terminates. Most programs designed for continuous running need to allocate temporary memory of variable size, without a specific fame for that memory's life time. It is obvious that if you request memory and do not return it for a significant amount of time, your program will run out of memory, and terminate when it requests additional memory.

With this said, you can go long way without using destructors in C++ by allocating everything that you can allocate in the automatic area. The only time when you really need to use dynamic memory is when the lifetime of the object must extend past its allocation scope, but even then C++ containers would take care of most allocations for you (of course the implementation of the standard containers relies heavily on the constructor/destructor infrastructure of the C++ language).

Destructors are guaranteed to be called if RAII is used. It is not like we have to use it, but it is generally a good idea to benefit from RAII because it allows for automatic resource management. In other words, if you write your program right, it won't have resource or memory leaks, so you don't even have to worry about it.

This is true not only for C++, but for other languages that have support for automatic resource management such as C#, Java, and even C (through non-standard extensions).

Basically, you need to read some book on C++ to understand the concept, probably. I also wrote a little article that might be helpful, see here.

Hope it helps :)

First of all, delete and delete[] are not destructors, they just call the destructors of the instances that are deleted (given that those do have destructors).

To answer your bigger question: The destructor of a class can do more than just release memory. For example, it might signal another program that it is about to close a connection or something.

Also, some programs run "forever" - or at least for as long as possible. I, for example, develop programs that are running on servers and they (hopefully) run for month. I do need to release memory (or other resources) as soon as possible otherwise the process will grow and eventually crash when the machine runs out of memory.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top