lint on malloc calls

Rich Salz rsalz at bbn.com
Tue Sep 13 00:08:33 AEST 1988


Juergen "Gandalf" Wagner, <gandalf at csli.stanford.edu>, asks how you
can make lint happy if one program has the following three lines:
	cp = (char *) malloc(10*sizeof(char));
	sp = (short *) malloc(10*sizeof(short));
	dp = (double *) malloc(10*sizeof(double));

He suggests this:
	#ifdef	lint
	extern char *malloc_char();
	extern short *malloc_short();
	extern double *malloc_double();
	#else	/* !lint */
	#   define malloc_char   malloc
	#   define malloc_short  malloc
	#   define malloc_double malloc
	#endif	/* lint */

On the systems I use, which are mostly BSD derivatives, lint will keep
quiet if it sees you casting a "worst-case" pointer into something not
so bad.  I you write your own wrapper around malloc (which is usually
a good idea anyway), you can get the lint natterings down to one line.

In system_header.h
	typedef int WORST_CASE;
	extern WORST_CASE *Mallocator();
in alloc.c
	#include "system_header.h"
	WORST_CASE *
	Mallocator(count)
	    int count;
	{
	    char *malloc();
	    WORST_CASE p;

	    if (p = (WORST_CASE *)malloc(count))
		return(p);
	    YelpAndDie();
	}

I remember this trick also working on Version7 lints.
It's been a long time since I've used SysV-
-- 
Please send comp.sources.unix-related mail to rsalz at uunet.uu.net.



More information about the Comp.lang.c mailing list