Immoral bugs

Doug Gwyn <gwyn> gwyn at brl-tgr.ARPA
Sun Feb 2 10:00:53 AEST 1986


> I thought I'd point out a couple of minor bugs in the fast "immoral"
> routines that were just posted...

Great!  I hope people get the point about "clever" code..

#include <malloc.h>
#include <memory.h>

void *
calloc( unsigned nelem, unsigned elsize )
	{
	register void *ptr;

	nelem *= elsize;

	if ( (ptr = malloc( nelem )) != 0 )
		(void) memset( (char *)ptr, 0, nelem );

	return ptr;
	}

Note that calloc() is pretty much useless.  How often do you really
want a malloc()ed block of storage filled with NUL characters rather
than some other sort of 0, e.g. NULL pointers, floating-point 0.0s,
etc.?



More information about the Comp.lang.c mailing list