Path: utzoo!utgpu!news-server.csri.toronto.edu!mailrus!uunet!motcid!henley From: henley@motcid.UUCP (Aaron Henley) Newsgroups: comp.lang.c Subject: Re: free (NULL); Keywords: free Message-ID: <2855@mica6.UUCP> Date: 9 May 90 15:04:59 GMT References: <1194@wet.UUCP> Reply-To: motcid!henley@uunet.uu.net Organization: Motorola Inc., Cellular Infrastructure Div., Arlington Heights, IL Lines: 42 noah@wet.UUCP (Noah Spurrier) writes: >char *squirl() >{ > static char *test = NULL; > free (test); > test = (char *) malloc (100); > return (test); >} >This function is run many times so iI do not want to protect it with an if >because the if would only be useful for the first time it is run, after that >it just eats up run time. > if (test != NULL) > free (test); >Will angry things happen if I try to free(NULL) ? ANSI C may specify that free(NULL) should work, but for those people currently not using an ANSI C compiler the following will also work: static char *test = (char *) malloc (100); This obviously wastes time during the first execution, but should work across all K&R C libraries and ANSI C libraries. BTW: If the ANSI C library supports this, does this mean that the "if (test != NULL)" is done by the library which now means that you'll always use up your run time with this test even if you know the test isn't needed? -- ___________________________________________________________________ / Aaron Henley, Motorola Inc. DISCLAIMER: std.disclaimer / / Cellular Infrastructure Division UUCP: ...!uunet!motcid!henley / /__________________________________________________________________/