Aricent Interview Question
Testing / Quality AssurancesCountry: India
Interview Type: In-Person
a memory leak occurs when a computer program incorrectly manages memory allocations.In object-oriented programming, a memory leak may happen when an object is stored in memory but cannot be accessed by the running
ex-
#include <stdlib.h>
void function_which_allocates(void) {
/* allocate an array of 45 floats */
float * a = malloc(sizeof(float) * 45);
/* additional code making use of 'a' */
/* return to main, having forgotten to free the memory we malloc'd */
}
int main(void) {
function_which_allocates();
/* the pointer 'a' no longer exists, and therefore cannot be freed,
but the memory is still allocated. a leak has occurred. */
}
In case of dynamic memory allocation in C/C++ if you have lost all the references/pointers to a memory allocated using malloc/calloc OR new, new[] etc. then this memory can never be freed/deleted because no reference is available to this memory and memory is said to be leaking. This memory can not be allocated to store some other thing by the program.
A shell script can only be used to find memory leak by static code analysis of the program This can be done by grepping the number of malloc/new etc and matching them with the number of free/delete etc. In case there are more malloc/new then then the program might contain a memory leak.
In case of dynamic memory allocation in C/C++ if you have lost all the references/pointers to a memory allocated using malloc/calloc OR new, new[] etc. then this memory can never be freed/deleted because no reference is available to this memory and memory is said to be leaking. This memory can not be allocated to store some other thing by the program.
a memory leak occurs when a computer program incorrectly manages memory allocations. A memory leak may happen when an object is stored in memory but cannot be accessed by the running code
- Wendy April 08, 2014