delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/10/03/19:15:28

From: firewind <firewind AT metroid DOT dyn DOT ml DOT org>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Memory leak.
Date: 3 Oct 1997 22:38:05 GMT
Organization: Netcom
Lines: 34
Message-ID: <613s4d$hrm@dfw-ixnews3.ix.netcom.com>
References: <3 DOT 0 DOT 16 DOT 19971003101449 DOT 36e7aec2 AT hem1 DOT passagen DOT se>
NNTP-Posting-Host: elp-tx1-07.ix.netcom.com
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

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 -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019