Passing types to a subroutine

Stephen J. Roznowski sjr at mimsy.UUCP
Tue Nov 15 09:18:54 AEST 1988


In article <10600 at haddock.ima.isc.com> karl at haddock.ima.isc.com (Karl Heuer) writes:
>In article <32119 at bbn.COM> mesard at bbn.com (Wayne Mesard) writes:
>>Or, how 'bout using a good old union to get rid of the extra param:
>>   union ptr { float *f; double *d; };
>>   main() {
>>       float f[10]; double d[10];
>>       sub(0, f); sub(1, d);
>>   }
>>   void sub(t, p) enum whichtype t; union ptr p; { ... }
>
>This is not correct code.  The second parameter is declared to be `union ptr',
>but the type actually passed is one of its member types.  This will fail if,
>for example, the implementation passes scalars on the stack and structs/unions
>with some other mechanism.  To do this `right', you'd have to declare a union
>object, copy the value `f' or `d' into its appropriate member, and then pass
>the union as the actual argument.

I have a problem with using unions. (actually two problems.)

One is since I do not know the type until run time, I need
to create a union that contains EVERY possible type.  Now
what happens if I want to pass an array that is inside of an
array of structures? (for example)

Second, it would not be unreasonable to have someone
call this routine with a 10,000 element array of 128 bit
double floating point values.  I do not want to have to copy
this data at any time, and total storage (in memory) needs
to be kept as low as possible. [Of course these subroutines
need to be as fast as possible]

Stephen
-- 
Stephen J. Roznowski                 sjr at mimsy.umd.edu



More information about the Comp.lang.c mailing list