realloc()....

michael sweet sweetmr at SCT60A.SUNYCT.EDU
Sun Feb 17 12:40:25 AEST 1991


> ... how come calling realloc() with a NULL pointer bombs....

Well, that's one of those nice functions which was never standardized for
that sort of thing- *most* libraries do take a NULL pointer happily, but
then some don't.  Both K&R 1 and 2 fail to say anything on how to deal with
NULL pointers and realloc() :(

One easy way around it is to use a macro like:

 #define REALLOC(p,n)  ((p) ? realloc((p),(n)) : malloc((n)))

(The extra parens are needed for old compilers)

In any case, this adds some safety at the expense of a few bytes of code;
this is also something that could be #ifdef'd for brain-dead systems so
smart systems don't suffer the overhead....

 -Mike Sweet

------------------------------------------------------------------------------
"The only        TASC                      (315) 724-1100 (voice)
 truth is that   555 French Road           (315) 724-2031 (fax)
 there are no    New Hartford, NY  13413   Internet: sweetmr at sct60a.sunyct.edu
 truths, only beliefs."                    Delphi:   DODGECOLT
------------------------------------------------------------------------------



More information about the Comp.sys.sgi mailing list