FAQ - malloc array - clarify

Karl Heuer karl at haddock.ima.isc.com
Wed Sep 12 07:18:45 AEST 1990


In article <1806 at tuvie> hp at vmars.tuwien.ac.at (Peter Holzer) writes:
>cpcahil at virtech.uucp (Conor P. Cahill) writes:
>>>No! Don't fix your code, which is correct. Fix the lint library...
>>The argument to malloc is defined in the library (i.e. where malloc()
>>is encoded).  running lint against the source builds the lint library and
>>therefore the lint library will correctly reflect what the code has (besides,
>>without code, you can rebuild a lint library).
>
>The lint libraries are ASCII files on our system. Are they in a compiled 
>form on yours ?

Common practice on modern machines is to supply a compiled form (llib-lc.ln)
as well as the ASCII form (llib-lc).  You probably have both, and just never
noticed.

>[The problem is that sizeof() incorrectly returns a signed int, and size_t is
>defined that way in <sys/types.h>, but malloc() expects an unsigned int.]
>Now if I want to shut up lint about [malloc(sizeof(x))], what should I cast
>the argument to malloc to?

Don't cast it at all!  The idea is to make the code *correct*, not to get rid
of spurious warnings caused by a known compiler/lint bug.

>[You could change the lint library to declare the argument to be int]

This would create a new warning for correct code (malloc(n*sizeof(T)), where n
is an unsigned variable).  Better to fix the compiler, if possible.

>(The other possibility is not to use lint, which I am forced to do, because
>lint gags on my prototypes, but in general, I would not recommend that).

Lint is commonly implemented as a shell script.  Find the place where it does
something like "cpp | lint1", and insert a filter that converts ANSI-isms to
forms that are acceptable to K&R-based lint.  (I currently use a sed script to
change "void *" to "char *", and a deprotoizer that's tuned to my personal
coding style.)

Karl W. Z. Heuer (karl at kelp.ima.isc.com or ima!kelp!karl), The Walking Lint



More information about the Comp.lang.c mailing list