Help me cast this!

Niels J|rgen Kruse njk at diku.dk
Thu May 5 21:05:57 AEST 1988


In article <7822 at brl-smoke.ARPA>, gwyn at brl-smoke.ARPA (Doug Gwyn ) writes:
> In article <294 at fedeva.UUCP> wrd3156 at fedeva.UUCP (Bill Daniels) writes:
> >How do I cast the malloc() in line 12 of the following program to avoid
> >the lint cries of "warning: illegal pointer combination" et al?
> (..)
> it.  The best you can do is obtain a "possible pointer alignment problem"
> warning.  That's because "lint" does not know that the pointer returned by
> malloc() is guaranteed to point to properly aligned storage for any use.
                                     ^^^^^^^^^^^^^^^^

    I have been wondering why malloc() returns a (char *), when
it is guaranteed to satisfy all alignment requirements. It
would seem to be more proper to return (maxalign *), where
maxalign is a type with the strictest alignment requirements of
all types. This would inform lint that the cast is safe.
In addition, passing (char *) is inefficient on some
architectures, whereas passing (maxalign *) would be efficient on
all architectures.

    I dont know anything about ANSI-C, other than what i have
picked up in this newsgroup. I gather that malloc has been
redefined as void *malloc(). Is a (void *) guaranteed to
satisfy all alignment requirements?
If so i would hope that this is reflected in the
representation, so that passing a (void *) is the most
efficient way to pass a pointer.
Changing declarations of malloc in existing code would be a
small price to pay for getting this cleaned up.


        Niels J|rgen Kruse
        stud. scient. at DIKU.
        (DIKU ~= CS dept. of University of Copenhagen)



More information about the Comp.lang.c mailing list