what are the reasons for memory leakage in C C++ (except the usual allocating memory and forget to deallocate it)
DEFINITION A memory leak is the gradual deterioration of system performance that occurs over time as the result of the fragmentation of a computer's RAM due to poorly designed or programmed applications that fail to free up memory segments when they are no longer needed.
Memory leaks occur when new memory is allocated dynamically and never deallocated. In C programs, new memory is allocated by the malloc or calloc functions, and deallocated by the free function.
If an exception is raised between allocation and deallocation, memory leak will occur.
void f1() {
int* ptr = new int;
// do something which may throw an exception
// we never get here if an exception is thrown
delete ptr;
}
Each time f1 terminates with an exception, 4 bytes are leaked (assuming int is 4 byte).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With