Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP
Posting-Version: version B 2.10.2 9/18/84; site rlgvax.UUCP
Path: utzoo!watmath!clyde!bonnie!akgua!sdcsvax!dcdwest!ittvax!decvax!genrad!panda!talcott!harvard!seismo!rlgvax!guy
From: guy@rlgvax.UUCP (Guy Harris)
Newsgroups: net.lang.c
Subject: Re: A useful construct for C ?
Message-ID: <448@rlgvax.UUCP>
Date: Sat, 9-Feb-85 13:16:43 EST
Article-I.D.: rlgvax.448
Posted: Sat Feb  9 13:16:43 1985
Date-Received: Mon, 11-Feb-85 04:47:28 EST
References: <389@lll-crg.ARPA> <8095@brl-tgr.ARPA>
Organization: CCI Office Systems Group, Reston, VA
Lines: 31

> > I would like to sample the community on a possibly useful construct
> > 	typeof(foo)
> > which is the type of expression foo.  It is similar in utility to sizeof().
> >	....
> > It would be nice to be able to say:
> > 	int *foo;
> > 	foo = (typeof(foo))malloc(sizeof(*foo) * nelts);
> 
> Well first, would the C standard group's (void *)'s help here?

It would.  The cast to "typeof(foo)" isn't necessary for C (the compiler
knows enough to generate a coercion from the "char *" which, of course,
everybody declares "malloc" as (right?) to the "int *" that "foo" is).
It has no effect on "lint" here (S3 "lint") - in both cases, it bitches about
"illegal pointer combination" and "possible pointer alignment problem".
That's what the "void *" would eliminated - a "void *" is assumed to be
castable to any other pointer type, safely.  It's the responsibility
of the routine returning a "void *", or the programmer, to ensure that
the pointer is actually usable (e.g., that it's aligned on the most
restrictive boundary that the machine requires).

> Actually, MALLOC really should know what alignment requirement is
> required.  Right now it just takes the least common multiple of
> all data types.  Should a similar builtin function (be available)?

"lint" has a comment in it to the effect of "I wish we had 'alignof'".
That's exactly what you asked for; maybe it should be proposed to the
ANSI C Standards Committee?

	Guy Harris
	{seismo,ihnp4,allegra}!rlgvax!guy