Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!usc!samsung!xylogics!merk!spdcc!ima!haddock!karl From: karl@haddock.ima.isc.com (Karl Heuer) Newsgroups: comp.lang.c Subject: Re: FAQ - malloc array - clarify Keywords: malloc, Sun, lint Message-ID: <17960@haddock.ima.isc.com> Date: 11 Sep 90 21:18:45 GMT References: <8056@helios.TAMU.EDU> <1990Sep08.022034.8444@virtech.uucp> <1803@tuvie> <1990Sep11.002544.9151@virtech.uucp> <1806@tuvie> Reply-To: karl@kelp.ima.isc.com (Karl Heuer) Organization: Interactive Systems, Cambridge, MA 02138-5302 Lines: 38 In article <1806@tuvie> hp@vmars.tuwien.ac.at (Peter Holzer) writes: >cpcahil@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 , 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@kelp.ima.isc.com or ima!kelp!karl), The Walking Lint