Casting |void *| values (was: Help me cast this!)

Karl Heuer karl at haddock.ISC.COM
Thu May 12 16:18:34 AEST 1988


In article <1543 at rpp386.UUCP> jfh at rpp386.UUCP (The Beach Bum) writes:
>In article <7822 at brl-smoke.ARPA> gwyn at brl.arpa (Doug Gwyn) writes:
>>General, SVR2 "lint" will complain about casting the (char *) returned by
>>malloc() into other pointer types, and there seems to be no way around it.
>
>... what i learned was that if i declared "void *malloc();" then lint shut
>up.  this would seem to be the correct behavior for an ANSI conforming lint
>... well, karl, what you got to say???

Warnings are not specified in the Standard, but appendix A.5 lists "a few
of the more common situations", and can probably be used as a rough guide.

One of the (suggested?) warnings is "an implicit narrowing conversion is
encountered, such as ... a pointer to |void| to a pointer to any type of
object other than |char|".  This covers uncasted assignment of malloc().

The guaranteed alignment is a property of malloc(), not a general property of
|void *|.  It should probably be declared with a #pragma (or a lintpragma,
which could be spelled "/*ALIGNED*/" in the UNIX tradition).

I recognize that many people believe uncasted malloc() is now kosher, and that
many ANSI compilers/lints will agree.  I don't have to like it, and my own
code won't depend on such sloppiness.  If I were to write a compiler, I'd
probably make this warning an option.

Given the obvious declarations, "ip = memcpy(ip1, ip2, n)" is correct, but
"ip = memcpy(cp1, cp2, n)" is wrong.  The inability of the type system to
detect this at compile-time bothers me, but I'm not sure what to do about it,
other than [0] implement a smart #pragma, or [1] use type-specific macros
as a front end to memcpy() (I've sometimes done this for malloc()).

Karl W. Z. Heuer (ima!haddock!karl or karl at haddock.isc.com), The Walking Lint
Followups to comp.std.c.



More information about the Comp.lang.c mailing list