questions? wrote:
Keith Thompson wrote:
"questions?" <un************@hotmail.com> writes:
Ico wrote:
questions? <un************@hotmail.com> wrote:
>if I use malloc() in a function to allocate a space for my array of
>structures. I didn't free() them anywhere in the program.
>I found that I can still use that space after I come back to main().
That is how it is supposed to work. When you allocate memory with
malloc() or one of the similar functions, it will be available until you
call free(). A lot of C implementations will also free this memory for
you when your program exits, but don't count on that.
So, with malloc() function, it is static regardless in the sense that
whether you are in main or in a function, it is permanant,right?
"Static" isn't the right word for it.
The lifetime of a memory block allocated with malloc() doesn't depend
on where you call it.
There are three storage durations: "static" (for static or global
objects; these exist for the entire lifetime of the program),
"automatic" (for non-static objects declared locally to a function or
block; these cease to exist when the function or block finishes), and
"allocated" (for objects allocated by malloc(); these exist until
they're free()d).
Note that malloc() and free() aren't the only relevant functions, but
I'm too lazy to go into the details of calloc() and realloc().
And main() *is* a function. An object declared inside main() isn't
global; it's local to the function.
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
--
E-Mail: Mine is an /at/ gmx /dot/ de address.