InterviewSolution
Saved Bookmarks
| 1. |
What is a memory leak? How to avoid it? |
|
Answer» When we assign a variable it takes space of our RAM (either heap or RAM)dependent on the size of data type, however, if a PROGRAMMER uses a memory available on the heap and FORGETS to a delta it, at some point all the memory available on the ram will be occupied with no memory left this can lead to a memory leak. int main(){ char * PTR = malloc(sizeof(int)); /* Do some work */ /*Not freeing the allocated memory*/ RETURN 0;}To avoid memory leaks, you can trace all your memory allocations and think forward, where you WANT to destroy (in a good sense) that memory and place delete there. Another way is to use C++ smart pointer in C linking it to GNU compilers. |
|