Xref: utzoo comp.std.c:933 comp.lang.c:17247 Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!tut.cis.ohio-state.edu!bloom-beacon!adam.pika.mit.edu!scs From: scs@adam.pika.mit.edu (Steve Summit) Newsgroups: comp.std.c,comp.lang.c Subject: realloc Summary: realloc should handle NULL pointers Keywords: realloc NULL Message-ID: <10170@bloom-beacon.MIT.EDU> Date: 29 Mar 89 02:07:46 GMT Sender: daemon@bloom-beacon.MIT.EDU Reply-To: scs@adam.pika.mit.edu (Steve Summit) Lines: 30 Today, for the third time in half as many years, I got badly burned, and wasted lots of time, due to a bug in a certain vendor's implementation of realloc. It is an apparently well- kept secret that realloc is supposed to behave gracefully at a slightly special-cased boundary point: when handed a NULL pointer and a nonzero size, it acts essentially as a malloc. If you have ever implemented a C run-time library, or if you are implementing one now, or if you may ever implement one, or if you know anyone who falls into any of these categories, pay attention: begin your realloc() implementation with the equivalent of: char *realloc(ptr, size) char *ptr; int size; { if(ptr == NULL) return malloc(size); ... The ptr argument and realloc's return value may be void *'s, and the size argument may be an unsigned or a size_t; the essential point I am making is the test for NULL and the call of malloc. This is a short message, so that people who don't read long articles will see it. I'll hold further explanations and justifications for another day. Steve Summit scs@adam.pika.mit.edu