Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!usc!snorkelwacker.mit.edu!bloom-picayune.mit.edu!news From: scs@adam.mit.edu (Steve Summit) Newsgroups: comp.lang.c Subject: Re: Why use -lmalloc Message-ID: <1991Apr5.003428.21831@athena.mit.edu> Date: 5 Apr 91 00:34:28 GMT References: <2@bodedo.UUCP> <179@atesysv.UUCP> <869@epiwrl.UUCP> Sender: news@athena.mit.edu (News system) Reply-To: scs@adam.mit.edu Organization: Thermal Technologies, Cambridge, MA Lines: 30 In article <869@epiwrl.UUCP> nelson@wrl.epi.com (Ken Nelson) writes: > [-lmalloc] notified us when the program > tried to allocate a block of space of size 0. The > normal alloc just returned a bad address that we > used until we corrupted something... What kind of "bad address?" It is legal for malloc(0) to return a non-null pointer, but that pointer points to zero bytes that you may modify (or inspect), so it is not good for anything other than comparison, or handing to free() or realloc(). Evidently the code in question did the equivalent of char *p = malloc(0); p[3] = 'x'; That the -lmalloc package "complained" about the malloc(0) did in fact catch this bug, but note that it would not have caught the very similar char *p = malloc(1); p[3] = 'x'; There are debugging malloc packages which will catch this error (and -lmalloc may be one of them). Steve Summit scs@adam.mit.edu