Article View: comp.sys.amiga.tech
Article #3560Re: De-Allocating Memory?
From: John Meissen
Date: Wed, 09 May 1990 19:05
Date: Wed, 09 May 1990 19:05
27 lines
1451 bytes
1451 bytes
In article <X-8#!|_@masalla.fulcrum.bt.co.uk> S.J.Raybould@fulcrum.bt.co.uk (Simon Raybould) writes:
>You need a recursive routine to free the whole list, along the lines of
>
>freelist(ptr)
>struct listelement *ptr;
>{
> if(ptr->Next)
> freelist(ptr->Next); /* Recurse */
> if(ptr)
> free(ptr);
>}
This is exactly the routine the original poster had listed, except he didn't
bother to check the validity of 'ptr' before freeing it. In that respect,
your routine has a bug, because if there is a chance of the pointer being
invalid you should check it BEFORE dereferencing it:
if (ptr)
{
if (ptr-Next) freelist(ptr-Next);
free(ptr);
}
--
John Meissen ............................... Oregon Advanced Computing Institute
jmeissen@oacis.org (Internet) | "That's the remarkable thing about life;
..!sequent!oacis!jmeissen (UUCP) | things are never so bad that they can't
jmeissen (BIX) | get worse." - Calvin & Hobbes
Technical Details
Message-ID:
<492@oregon.oacis.org>Path:
arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!mailrus!uwm.edu!ogicse!oregon!jmeissenReferences:
<12750@wpi.wpi.edu> <X-8#!|_@masalla.fulcrum.bt.co.uk>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!mailrus!uwm.edu!ogicse!oregon!jmeissen From: jmeissen@oregon.oacis.org (John Meissen) Newsgroups: comp.sys.amiga.tech Subject: Re: De-Allocating Memory? Keywords: Lattice 5.04? Message-ID: <492@oregon.oacis.org> Date: 9 May 90 19:05:02 GMT References: <12750@wpi.wpi.edu> <X-8#!|_@masalla.fulcrum.bt.co.uk> Organization: Oregon Advanced Computing Institute (OACIS), Beaverton, OR Lines: 27