question about free()

Karl Heuer karl at haddock.ima.isc.com
Wed Aug 1 11:52:18 AEST 1990


In article <2958 at dftsrv.gsfc.nasa.gov> vander at nssdcb.gsfc.nasa.gov writes:
>does the free() function return anything?

No.

>in VAXC v3.0 it's defined as:  void free (void *ptr);
>i though it used to (just to tell you it was successful)
>  - was that an incorrect implementation ?

Since free() cannot fail unless the program is already seriously broken,
there's not much point.  It might as well use abort() to flag the error.

>if the argument is void *ptr how does it know how many bytes to release?

Magic.  (I mean that seriously.)  It's not something the user needs to be
concerned with; the implementation will remember the size somehow.

>which brings me to my example (attached) do i need to cast the pointer
>before free()ing it?

You should be okay as long as `free()' has been declared with a prototype.
(It's sufficient to #include <stdlib.h> if your compiler is ANSI-compliant.)
On a compiler without prototypes, you should cast the argument to `char *' (or
`void *' if you have it, but this is also an ANSI invention) for complete
portability.  (It looks like *this* code will never be ported off a vax
anyway, but it's good programming practice, and makes lint happy.)

>void RMS_free_XABchain( struct XABKEY *xabp )
>/* originally had as void *xabp but then you can't reference its elements */

If this is a pointer to a generic class of structs that all share a common
initial member, you could use `void *xabp' and reference the common member
with `((struct XABKEY *)xabp)->xab$b_cod'.

>	  case XAB$C_ALL : xabp = (struct XABALL *) xabp;  break;

This almost certainly isn't what you want.

Karl W. Z. Heuer (karl at kelp.ima.isc.com or ima!kelp!karl), The Walking Lint



More information about the Comp.lang.c mailing list