realloc((char *)NULL,size) - how standard ?

Mark William Hopkins markh at csd4.csd.uwm.edu
Tue Mar 5 03:37:44 AEST 1991


dave at cs.arizona.edu (Dave P. Schaumann) writes:
> Actually, if you want to port code that passes NULL to realloc to a machine
> that doesn't like that, all you have to do is this:
> [write correct routine called Realloc]
> Then, just #define realloc Realloc anywhere you need to.

In article <1991Feb25.053353.12842 at athena.mit.edu> scs at adam.mit.edu writes:
>This is the right approach, but it's much better (if you care
>about widespread portability) to use a name like xrealloc instead
>of Realloc.  "Realloc" is not a good name for a wrapper function
>around realloc, because it is not distinct under a monocase
>linker.

The realloc problem is a red-herring.  All you have to do hide this away
somewhere in an include file:

#define xalloc(Loc, Bytes) \
   ((Loc) == NULL? malloc(Bytes): realloc((Loc), (Bytes)))

and don't use anything but xalloc...

You could even add in an #if section to define xalloc as realloc for standard
C.  And while you're at it, you might as well put in a couple more definitions
like:

#define make(Loc, Type) ((Type *)xalloc(Loc, sizeof(Type)))
#define break(Loc) (free((char *)Loc))

just to fatten up the include file...



More information about the Comp.lang.c mailing list