Path: utzoo!utgpu!news-server.csri.toronto.edu!bonnie.concordia.ca!uunet!cs.utexas.edu!swrinde!elroy.jpl.nasa.gov!usc!apple!netcom!resnicks From: resnicks@netcom.UUCP (Steve Resnick) Newsgroups: comp.os.msdos.programmer Subject: Re: free() question Message-ID: <21715@netcom.UUCP> Date: 23 Jan 91 07:16:53 GMT References: <8822@star.cs.vu.nl> Organization: Netcom- The Bay Area's Public Access Unix System {408 241-9760 guest} Lines: 31 In article <8822@star.cs.vu.nl> tamboer@cs.vu.nl (Tamboer Erik) writes: >A question about the free() function in C. I'm posting it here because I >thought I might reach a few C programmers here ;-) > >Is it safe to free() a block of memory that has already been free()d? >I have a program that malloc()s trees of structs that may or may not be >free()d at some later point. Upon termination, it is important that they >are all released from memory. Is it safe to just free all trees, including >those that have already been freed, or do I have to resort to something >like changing every occurence of free() in the program by { free(tree); >tree = NULL } ? (as free(NULL) is supposed to be harmless). > >This is to be a portable program, so it must work with most implementations >of free(), compiler-independent. This is a frequently asked question in comp.lang.c, and really belongs there. No, it's not adviseable to free a block of memory which hasn't been malloced. If it's been free()'d, then it's not longer malloced. Hope this helps... Steve -- ------------------------------------------------------------------------------- resnicks@netcom.com, stever@octopus.com, steve.resnick@f105.n143.z1.FIDONET.ORG apple!camphq!105!steve.resnick, {apple|pyramid|vsi1}!octopus!stever - In real life: Steve Resnick. Flames, grammar and spelling errors >/dev/null 0x2b |~ 0x2b, THAT is the question. -------------------------------------------------------------------------------