Mail Archives: djgpp/1997/10/03/19:15:28
Peter Palotas <blizzar AT hem1 DOT passagen DOT se> wrote:
> Could someone please give me a complete description of what a memory leak
> really is?
A memory leak occurs when your program allocates memory, but "forgets" to
free it. For example, say you have
void mumble(void)
{
int *foo = (int *)malloc(1024 * (sizeof int));
do_some_stuff();
do_some_more_stuff();
...
return;
}
Notice that the memory pointed at by foo is never free()'d. This means that
every time mumble() is called, you will lose (1024 * (sizeof int)) amount of
memory. If your program calls mumble() often, you'll get into trouble because
eventually too much memory will have leaked.
A questionable solution is the use of alloca(), which is supposed to
automagically free() (well, not really, but the same effect) memory allocated
by it when the function returns. While it is easier on the programmer, its a
Bad Idea(tm). Popular implimentations do not work on all computers, and on
the x86 (meaning, DJGPP) it is implimented using the stack, so excessive use
of it will exhaust your stack space and cause the program to fail.
--
[- firewind -]
[- email: firewind AT metroid DOT dyn DOT ml DOT org (home), firewind AT aurdev DOT com (work) -]
[- "You're just jealous because the voices talk to -me-." -]
- Raw text -