| TIP: Click on subject to list as thread! | ANSI |
| echo: | |
|---|---|
| to: | |
| from: | |
| date: | |
| subject: | Memory matters |
Hello Pascal!
Oct 19 16:34 03, Pascal Schmidt wrote to Bo Simonsen:
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?
PS> The problem with your above code is that you do free(tmpStruct) and
PS> tmpStruct->then access next, but you don't know what free() does, it
PS> could for example overwrite the contents of tmpStruct->next. Usually
PS> it doesn't, but you cannot be certain.
You have a point. I just used to write linked list in that way, so I forgot
all about I needs to be written in another way for freeing..
curStruct = firstStruct;
do
{
tmpStruct = curStruct->next;
free(curStruct);
curStruct = tmpStruct;
} while(curStruct);
PS> You could even want to have a free() that zeroes memory or writes
PS> some pattern to it to catch "use after free" bugs
semi-automatically.
PS> The Linux kernel, for example, has stuff like this in its linked list
PS> code. Deleting a list item writes special "poison" values into its
PS> prev and next pointers, so that the rest of the code can find out
PS> when it is being passed a list element that was supposed to be
PS> deleted already.
Ok.
Regards,
Bo
--- Msged/LNX 6.1.2 (Linux/2.4.18-586tsc (i586))
* Origin: The Night Express, Roennede Dk (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™.