malloc()

KW Heuer kwh at bentley.UUCP
Wed May 14 11:42:58 AEST 1986


In article <866 at ttrdc.UUCP> ttrdc!levy (Dan Levy) writes:
>Y'know--while I'm thinking about it, why IS malloc() set up to return a
>(char *) pointer at all, if that IS inviting a potential (or at least to
>lint's little mind) pointer alignment problem?  Since malloc() is supposed
>to return a maximally aligned pointer, seems to me that it should have
>been set up to return a (double *).  Yes, yes, before flames of misunder-
>standing start, I _KNOW_ that the convention for malloc() is to return
>a (char *) and that it returns a maximally aligned pointer just the same
>because of the way it has been written.  It just seems that the methodology
>was back-a**wards, that at least returning a (double *) would indicate an
>implicit promise to the user (and to lint-like checking) that the return
>value WAS maximally aligned.  The only problem I could see with this kind
>of scheme is that some systems might not support double.  But that could
>be gotten around by #defining a malloc_t type that would represent the
>maximally aligned data type of the machine at hand.

This is what I started to mention once before, and picked up a lot of "no,
you don't understand how it works" messages (sigh).  I saw the need for a
"ALIGN *" datatype, and was hoping "void *" would be it.  (Nope, just a
useless synonym for "char *").  The idea would be that, just as it's always
legal to cast any pointer into a "char *", it would be legal to cast *from*
"ALIGN *" to any pointer.  Then lint would shut up about malloc()!

Also, having malloc() return a real "ALIGN *" would be convenient for word-
addressible machines where "char *" requires extra bits.

Btw, if I really want to reduce the signal/noise ratio in lint, I use
#if lint
extern char       *cmalloc();
extern int        *imalloc();
extern struct foo *fmalloc();
...
#else
extern char *malloc();
#define cmalloc(n) ((char       *)malloc((n)*sizeof(char))
#define imalloc(n) ((int        *)malloc((n)*sizeof(int))
#define fmalloc(n) ((struct foo *)malloc((n)*sizeof(struct foo))
...
#endif

Karl W. Z. Heuer (ihnp4!bentley!kwh), The Walking Lint



More information about the Comp.lang.c mailing list