lint on malloc calls

Juergen Wagner gandalf at csli.STANFORD.EDU
Mon Sep 12 12:37:51 AEST 1988


In article <9900007 at bradley> brian at bradley.UUCP writes:
>
>> /* Written  3:12 pm  Sep  8, 1988 by jgb at linus.UUCP */
...
>> 	entry *nextentry;
>>
>> 	nextentry = (entry *) malloc(sizeof(entry));
>> 
>> Running lint -bach yields:
>> 
>> 	phone.c(61): warning: illegal pointer combination
>> 	phone.c(61): warning: possible pointer alignment problem
...
>
>  You probably didn't tell lint or cc what malloc() returns, so it
>assumed int malloc()... The warning is given because you have done
>an explicit cast of a pointer to an integer. Just add the following
>line to either the top of your program or the top of your function:
>
>extern char *malloc();

That might well be. If you don't use the above declaration lint will 
complain. Yet, it should say that this is an "illegal combination of
pointer and integer", not what you read above.

>  Or, if you compiler supports void*
>
>extern void *malloc();
>

What is that supposed to do? Casting that to a (char *) I got a warning
because (void *) objects may not be byte-addressable.

Still, I think, all this doesn't account for one big problem: what do you
do if you have a program like

	cp = (char *) malloc(10*sizeof(char));
	sp = (short *) malloc(10*sizeof(short));
	dp = (double *) malloc(10*sizeof(double));

where malloc indeed returns values of different type. Assuming (double *)
fits them all isn't sufficient. This might break on a machine with different
representations for word and for byte pointers. I don't think, there is any
other way than to make lint happy by saying

# 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

That's ugly but didn't come across any panacea for this problem. If other 
people have other suggestions, please let me know.

-- 
Juergen "Gandalf" Wagner,		   gandalf at csli.stanford.edu
Center for the Study of Language and Information (CSLI), Stanford CA



More information about the Comp.lang.c mailing list