We allocate memory in C using malloc and in C++ using new. I know that memory allocated must
be freed or given back to OS using free in C and delete in C++.
If I forgot to use free/delete after allocating memory, it means there will be memory leak.
Now, my question is, is this memory leak only during the time period of execution of program; or is it permanent leak/loss or is it gained again once I restart the system ? What is the internal process actually ? What does memory leak/loss mean exactly?
I would be very grateful if someone could explain this in detail or provide me with some nice references.
UPDATE 1
After reading some answers, I learnt that memory is given back to OS/system after program terminates, if that is so, why everyone needs to care so much about memory leak, and why preventing memory leak is very important ?
UPDATE 2
So, memory leak should be prevented so that the system doesn't crash due to lack of sufficient memory for allocation purposes ??
UPDATE 3
So, after reading all answers, I realized that memory leak is quiet important issues to prevent system crashes. But, for a beginner like me, how can I be sure if my program is completely free from memory leak. I try to do free,delete if I am using malloc,new but sometimes, it gets messy. Is there any tool or method which I can use to know if my program is doing any memory leak ?
UPDATE 4
After reading answers, I have now understood the importance of memory leak free code, less use of new/delete, more use of STL, learnt new stuffs like RAII,valgrind and good programming practices.
Thanks to all :)
question from:
https://stackoverflow.com/questions/1232262/memory-leak-in-c-c-forgot-to-do-free-delete 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…