hiding lint's ravings (was Re: FAQ - malloc array - clarify)

Dave Martindale dave at imax.com
Fri Sep 14 13:45:28 AEST 1990


In article <2985 at anasaz.UUCP> duane at anasaz.UUCP (Duane Morse) writes:
>Here are two suggestions to remove lint's pointer alignment complaint.
>
>2) Write your own "malloc" subroutine which returns 0 for success and
>a nonzero value for failure; pass a pointer to where you want to store
>the address and have the routine interpret it as a char **; use a
>variable of the type you really need (struct whatever *) and cast it
>to (char **) in the subroutine call -- lint won't complain about any
>of this.

This is a BAD idea.  If you are on a machine where a byte and (short,
int, long, float, double) pointers do not have the same representation,
this won't work.  When you declare the "extra" allocator argument
as a char **, and then store the memory address via it, the memory
allocator will always store the address in char * format - it doesn't
know what actual type of pointer you want to use.

In contrast, even though lint complains, when you use malloc in the
normal way, the C compiler knows that malloc is returning a char *
and you want a different type of pointer, and will generate code to
do the conversion if any is needed on this machine.

So, your suggestion gets rid of the lint complaint, but gives code
that is no longer portable.  If I were marking such an assignment,
I'd mark that as an error.



More information about the Comp.unix.programmer mailing list