A useful construct for C ?

Kevin Martin kpmartin at watmath.UUCP
Sun Feb 10 07:30:38 AEST 1985


>> It would be nice to be able to say:
>> 	int *foo;
>> 	foo = (typeof(foo))malloc(sizeof(*foo) * nelts);
>
>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 (I'll use a
>silly name here to avoid confusion:
>
>	foo = (typeof foo) malloc(sizeof(*foo)*nelts, rons_typeof foo);
>
>Where rons_typeof would yield something like an enum that could be checked
>for in the Malloc routine.
>
>-Ron


This sounds like a job for an 'alignof' operator:
    foo = (typeof foo) malloc(sizeof(*foo)*nelts, alignof(*foo) );

In addition, several implementations require a method of finding the
alignment of a type in order to implement <varargs.h> (or <stdarg.h>,
depending who you ask).
               Kevin Martin, UofW Software Development Group



More information about the Comp.lang.c mailing list