Path: utzoo!news-server.csri.toronto.edu!cs.utexas.edu!usc!zaphod.mps.ohio-state.edu!pacific.mps.ohio-state.edu!linac!uwm.edu!csd4.csd.uwm.edu!markh From: markh@csd4.csd.uwm.edu (Mark William Hopkins) Newsgroups: comp.lang.c Subject: Re: realloc((char *)NULL,size) - how standard ? Message-ID: <9908@uwm.edu> Date: 4 Mar 91 17:37:44 GMT References: <15112:Feb2419:11:4391@kramden.acf.nyu.edu> <941@caslon.cs.arizona.edu> <1991Feb25.053353.12842@athena.mit.edu> Sender: news@uwm.edu Organization: University of Wisconsin - Milwaukee Lines: 29 dave@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@athena.mit.edu> scs@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...