fast code and no morals

gwyn at brl-tgr.UUCP gwyn at brl-tgr.UUCP
Thu Jan 23 03:21:58 AEST 1986


> Here's a version of calloc that breaks every rule of "structured"
> programming ever invented -- and runs about twice as fast as a
> vanilla version:
> 
> /* calloc - allocate and clear memory block */
> #define CHARPERINT (sizeof(int)/sizeof(char))
> #define NULL 0
> 
> char *calloc(num, size)
> unsigned num, size;
>     {
>     register char *mp;
>     char *malloc();
>     register int *q, *qlim, m;
> 
>     num *= size;
>     mp = malloc(num);
>     if (mp == NULL) return (NULL);
>     q = (int *) mp;
>     qlim = (m = (num+CHARPERINT-1)/CHARPERINT) + (q = (int *)mp);
> 
>     switch (m & 7)
> 	do	{
> 		    *q++ = 0;
> 	    case 7: *q++ = 0;
> 	    case 6: *q++ = 0;
> 	    case 5: *q++ = 0;
> 	    case 4: *q++ = 0;
> 	    case 3: *q++ = 0;
> 	    case 2: *q++ = 0;
> 	    case 1: *q++ = 0;
> 	    case 0: ;
> 	    } while (q < qlim);
> 
>     return (mp);
>     }

So what?  This can be written "structured", and would be more
maintainable if it were.  Surely this is not an example of
recommended practice?



More information about the Comp.lang.c mailing list