In C++, dynamically allocated memory has to be de-allocated manually by the code author through the delete
operator. Otherwise, issues like
C++ | C# |
Manual memory de-allocation is through the | Automatic memory de-allocation is through a garbage collector. |
The code author explicitly decides which memory is to be de-allocated. | The garbage collector automatically de-allocates the memory not being used in the program. |
The code author has complete control of the de-allocation of dynamically allocated memory. | The code author has no responsibility for the de-allocation of memory; the garbage collector runs periodically. |
Destructor is called against the delete operator. | There is no need to implement the destructor explicitly. |
Issues like memory leakage and a dangling pointer can occur due to manual memory management. | There is no need to take care of memory leakage and dangling pointer issues due to garbage collection. |
The code author knows when the memory was de-allocated, and the destructor was called against the delete operator. | The code author does not know when the memory was de-allocated, and the destructor was called. |
There is no time taken for the periodic run of the garbage collector, so it is fast in performance. | The periodic run of the garbage collector takes time to find the memory that is no more in use and needs to be de-allocated. |