Error Notes
C++
04-memory-leak

C++ Error Notes

Memory Leak error

Issue

Failure to deallocate dynamically allocated memory, leading to a gradual depletion of available memory.

Details

Memory leaks occur when dynamically allocated memory is not released back to the system after it is no longer needed. This often happens when the new operator is used to allocate memory, but the corresponding delete operator is not called to free the memory.

void func() {
    int* ptr = new int;
    // Memory allocated but not deallocated
}

Solution

To prevent memory leaks, always pair each call to new with a corresponding call to delete.