A useful construct for C ?

Guy Harris guy at rlgvax.UUCP
Sun Feb 10 04:16:43 AEST 1985


> > 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



More information about the Comp.lang.c mailing list