TIP: Click on subject to list as thread! ANSI
echo: c_echo
to: Bo Simonsen
from: Pascal Schmidt
date: 2003-10-19 16:34:18
subject: Memory matters

Hi Bo! :-)

 BS>>> for(tmpStruct=firstStruct; tmpStruct; tmpStruct=tmpStruct->next)
 BS>>> {
 BS>>>    free(tmpStruct);      
 BS>>> }
 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...
 BS> So you set it to NULL after freeing it?
The problem with your above code is that you do free(tmpStruct) and then
access tmpStruct->next, but you don't know what free() does, it could
for example overwrite the contents of tmpStruct->next. Usually it
doesn't, but you cannot be certain.

You could even want to have a free() that zeroes memory or writes some
pattern to it to catch "use after free" bugs semi-automatically.
The Linux kernel, for example, has stuff like this in its linked list code.
Deleting a list item writes special "poison" values into its prev
and next pointers, so that the rest of the code can find out when it is
being passed a list element that was supposed to be deleted already.

Ciao
Pascal

--- Msged/LNX 6.1.1
* Origin: How come you know my name? (1:153/401.2)
SEEN-BY: 633/267 270
@PATH: 153/401 307 140/1 106/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™.