lint on malloc calls

Dieter Woerz woerz at iaoobelix.UUCP
Wed Sep 14 08:51:18 AEST 1988


In article <9900007 at bradley> brian at bradley.UUCP writes:
>
>> /* Written  3:12 pm  Sep  8, 1988 by jgb at linus.UUCP */
>
>       [ edited for brevity ]
>
>>
>>      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
>>
>> What am I doing wrong?  Doesn't K+R suggest this on pp. 133-134 (at
>> the end of section 6.5)?
>
>  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:

This isn't the cause here, because, if you don't tell lint, that
malloc returns a char *, lint will complain with a message:

    warning: illegal combination for pointer and integer, op =

The cause is, that jgb at linus.UUCP asked lint with the a flag to (I
cite the lint manual on Ultrix V2.2, the one I've handy)

    Complain about casts which have questionable portability.

The problem is, that lint doesn't know, that malloc always returns a
best aligned pointer instead of (as char * normally can) a possibly
worst aligned one. This would be of "questionable portability" for
other systems.

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

This would only help, if you lint library, where malloc is defined,
is changed too for malloc returning void *. But I think lint must be
changed for this one too, as lint must know that a void * will always
be aligned properly. I'm not sure, if the current lint's know this
definition.

------------------------------------------------------------------------------

Dieter Woerz
Fraunhofer Institut fuer Arbeitswirtschaft und Organisation
Abt. 453
Holzgartenstrasse 17
D-7000 Stuttgart 1
W-Germany

BITNET: iaoobel.uucp!woerz at unido.bitnet
UUCP:   ...{uunet!unido, pyramid}!iaoobel!woerz



More information about the Comp.lang.c mailing list