malloc+copy+free vs realloc (was Efficient coding)
Walter Bright
bright at Data-IO.COM
Tue Nov 1 05:42:24 AEST 1988
In article <14232 at mimsy.UUCP> chris at mimsy.UUCP (Chris Torek) writes:
>There is, however, one case where I do use
>malloc+copy+free instead of realloc: If the data are important and
>must not be destroyed by a failed realloc. The draft standard says
>that this is already so; but I have my doubts about current implementations.
In which case, as I said, you write your own realloc(), as in:
#if BADREALLOC
extern void *mem_realloc();
#else
#define mem_realloc(p,oldsize,newsize) realloc((p),(newsize))
#endif
and write my_realloc to do the malloc/copy/free. This gives you the advantage
of using a well-implemented realloc with a fallback to your own if the system
one is bad.
I have used a (slightly more complex) variation on this for all my software
for years, and have been pleased with the results. I try to code in ANSI
as much as possible, and use the macro preprocessor for compatibility with
stone age compilers. I also port the ANSI library routines when I need them
for some port, instead of trying to do without them.
More information about the Comp.lang.c
mailing list