Why use -lmalloc

Steve Summit scs at adam.mit.edu
Fri Apr 5 10:34:28 AEST 1991


In article <869 at epiwrl.UUCP> nelson at 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 at adam.mit.edu



More information about the Comp.lang.c mailing list