Path: utzoo!mnetor!uunet!mcvax!diku!njk From: njk@diku.dk (Niels J|rgen Kruse) Newsgroups: comp.lang.c Subject: Re: Help me cast this! Message-ID: <3802@diku.dk> Date: 5 May 88 11:05:57 GMT References: <294@fedeva.UUCP> <7822@brl-smoke.ARPA> Organization: DIKU, U of Copenhagen, DK Lines: 33 Summary: why is malloc defined as char *malloc()? In article <7822@brl-smoke.ARPA>, gwyn@brl-smoke.ARPA (Doug Gwyn ) writes: > In article <294@fedeva.UUCP> wrd3156@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)