questions? said:
Thank you all for the answer!!! It is really really helpful.
1) what is really a memory leak?
Ask wikipedia or the jargon file
http://en.wikipedia.org/wiki/Memory_leak
http://www.catb.org/jargon/html/M/memory-leak.html
2) if I forgot to free a memory from malloc(), after termination of my
program. It will be returned back to system,right?
Yes, if your system does this. And as long as the usage of your
code does not change.
Imagine that you wrote a module and a "test" programme for it.
#include "myhappyfunmodule.h"
void test (void)
{
/* use myhappyfunmodule stuff */
/* a) myhappyfunmodule has an internal memory leak */
/* b) myhappyfunmodule returns allocated storage and
** test() does not free() it */
}
int main (void)
{
test();
return 0;
}
Now, one year after having written it, you want to use
myhappyfunmodule for a programme which runs for hours and
days in a loop. You thoughtlessly copy "test", throw out
main(), rename test() to runhappyfun(), change a couple
of things and try it out.
runhappyfun() is called millions of times and leaks a
couple of bytes every time; this effectively becomes a problem
over time as the programme eats your resources. You may have
to terminate it early/it dies by itself/it takes down your
system and valuable data.
Now, you have a look into runhappyfun(), find b), remove it.
Seems to work.
a) still leaks a few bytes at a time but maybe not always.
Then comes the one time it really counts and, sadly, a) eats
your memory once again and does Bad Things.
So, you have to debug your old code, find a) and fix it.
Maybe you even knew about the leaks when writing
myhappyfunmodule but thought "test() is only a test function,
I do not want to bother with deallocation" and "well, in a
very special situation, we have a leak (a)) but preventing
this costs ten lines extra which I always can do later",
respectively...
If you do it right from the beginning, you can save yourself
trouble further down the road.
Cheers
Michael