malloc, a temporary variable, and a linked list (what happens)

Ray Wallace wallace at ynotme.enet.dec.com
Thu Sep 20 05:30:41 AEST 1990


In article <20319 at orstcs.CS.ORST.EDU>, duvalj at bionette.cgrb.orst.edu (Joe Duval) writes...
>What I really want to know is what happens to the variable temp each time it
>is malloced?  Does it end up taking up a whole lot of memory or does the
>earlier malloced memory get lost?  How would I free this?

>			temp = (linelist *) malloc (sizeof(linelist));
>			indata[lines].points = temp;  /* retain the top of the list */

You don't want to free the memory that temp is pointing too. The memory is
being used to create your list, via indata[lines].points and
indata[lines].points->next which keep track of the malloc'ed address'.

If at some point you are done using the data from one or all of the lists
pointed to by indata[], then you could walk through each of the lists
(backwards) and free the memory.

---
Ray Wallace		
		(INTERNET,UUCP) wallace at oldtmr.enet.dec.com
		(UUCP)		...!decwrl!oldtmr.enet!wallace
		(INTERNET)	wallace%oldtmr.enet at decwrl.dec.com
---



More information about the Comp.lang.c mailing list