TIP: Click on subject to list as thread! ANSI
echo: c_echo
to: Roger Scudder
from: Bo Simonsen
date: 2003-10-18 23:54:32
subject: Memory matters

Good day Roger,

 > But if I've alot of elements, I need a linkedlist 
 RS> anyhow, and then global vars, would not effort me.
 > To free them I still need:
 > for(tmpStruct=firstStruct; tmpStruct; tmpStruct=tmpStruct->next)
 > {
 >    free(tmpStruct);      
 > }
 > Hmmm...

 RS>  Well, you don't want to try to access tmpStruct after 
 RS> you have free'd it.  I would do something more like 
 RS> this...

So you set it to NULL after freeing it?

 RS> #include "sniptype.h"  /* for macro FREE(x) */

 RS> myStruct = myStruct->head;

 RS> while ( myStruct->next != NULL )
 RS> {
 RS>     tmpStruct = MyStruct;
 RS>     myStruct = myStruct->next;
 RS>     FREE(tmpStruct);
 RS> }

 RS> FREE(myStruct);

#define FREE(x) free(x); x = NULL;

or?

 RS> Since you have a large number of pointers in your 
 RS> structure you should probably have a function to 
 RS> release the memory.  Based on the little bit that I 
 RS> have seen, I would put the structures and any functions 
 RS> that access them in a separate file.

I do so.

Regards,
Bo


--- Maximus/UNIX 3.03b
* Origin: The Night Express (2:236/100)
SEEN-BY: 633/267 270
@PATH: 236/100 237/9 20/11 106/1 2000 633/267

SOURCE: echomail via fidonet.ozzmosis.com

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.