STEINBERGER@KL.SRI.COM.UUCP (05/22/87)
I have a question concerning a problem that Fortran does not handle well. I would like to write a subroutine that gets as arguments the following: (1) An array (2) The type of array (byte, integer*2, integer*4, real*4, etc.) In the subroutine I want to write to a file (or otherwise manipulate) data from the passed array. Fortran has an obvious problem with data typing. My (klutzy) solution was to use OTS$MOVE3 to copy the data from ARRAY to another array of the appropriate data type, then do the write. Of course this wastes a fair amount of space for duplicate data, and wastes some time to move the data bytes to the new array. But it did work. . . . I'm a bit rusty in C and Pascal. Can anyone tell me if either of these (or another HLL) allows the user to cast an array into an arbitrary type at runtime and then read/write or otherwise manipulate it as such? Do I have to use MACRO to do what I want? Thanks to all who reply. -Ric Steinberger steinberger@kl.sri.com -------
ogud@SDAG.CS.UMD.EDU (Olafur Gudmundsson) (05/22/87)
>I have a question concerning a problem that Fortran does not handle well. >I would like to write a subroutine that gets as arguments the following: > > (1) An array > (2) The type of array (byte, integer*2, integer*4, real*4, etc.) > >In the subroutine I want to write to a file (or otherwise manipulate) data >from the passed array. Fortran has an obvious problem with data typing. > The simple solution to your problem is to use equivalence statements this way there is no overhead because of copying. The only overhead is that you will have to use IF statments to pick the right data to work with Example : subroutine foo( arr, typ) int arr( *) , typ real r4( 1) integer*2 i2(1) byte c1(1) eqivalence (arr(1), r4(1) , i2(1) , c1(1)) c and in the subroutine you do if( typ.eq. INT) then else if( typ .eq. REA4) then ... endif and so on ---------------------------------------------------------------------------- Spelling mistakes make life more colorful ____________________________________________________________________________ Olafur Gudmundsson Dept. of Computer Science University of Maryland ARPA: ogud@sdag.cs.umd.edu UUCP: {...}!seismo!mimsy!sdag!ogud Tel: (301)-454-6153 (w) UPS: College Park MD. 20742 ATT: (301)-595-4154 (h)
KVC@ENGVAX.SCG.HAC.COM (Kevin Carosso) (05/22/87)
> I'm a bit rusty in C and Pascal. Can anyone tell me if either of these > (or another HLL) allows the user to cast an array into an arbitrary type > at runtime and then read/write or otherwise manipulate it as such? Do I > have to use MACRO to do what I want? Thanks to all who reply. > -Ric Steinberger Through a non-standard extension, VAX Pascal allows type-casting. You could create a VAX Pascal routine that accepts the array and some indicator of the type and then operate on the elements of the array while casting them. For example, here's a simple Pascal program that treats an array as longwords or bytes... -------------------------------------------------------------- program z (input, output); type byte_array = array [0..7] of [byte] 0..255; long_array = array [0..1] of unsigned; var x : long_array := (1,2); i : integer; begin for i := 0 to 1 do writeln (x::long_array[i]); (* don't need the cast operator "::" here *) writeln; for i := 0 to 7 do writeln (x::byte_array[i]); end. -------------------------------------------------------------- /Kevin Carosso kvc%engvax@oberon.usc.edu Hughes Aicraft Co. kvc@engvax.scg.hac.com
u3369429@murdu.OZ (Michael Bednarek) (05/25/87)
In article <8705221905.AA01082@sdag.cs.umd.edu> ogud@SDAG.CS.UMD.EDU (Olafur Gudmundsson) writes: >The simple solution to your problem is to use equivalence statements >this way there is no overhead because of copying. >The only overhead is that you will have to use IF statments to pick the >right data to work with > >Example : > subroutine foo( arr, typ) > > int arr( *) , typ > real r4( 1) > integer*2 i2(1) > byte c1(1) > eqivalence (arr(1), r4(1) , i2(1) , c1(1)) > >c and in the subroutine you do > if( typ.eq. INT) then > > else if( typ .eq. REA4) then > > ... > > endif This is not only simple, it also doesn't work. I suspected that immediately, but I compiled the fragment above ... >Spelling mistakes make life more colorful [This is not funny] and got -F-MULDECNAM Multiple declaration of name, A name appears in two or more inconsistent declaration statements. Of course, the same approach within a main program will work. Michael Bednarek (u3369429@murdu.oz or U3369429@xvax.dn.mu.oz.au)
gac@drao.nrc.CDN.UUCP (05/26/87)
You will not be able to get away with that little overhead. An approach that does work is as follows: 1. In your calling program equivalence the array(s) to character strings and, in the subroutine calls pass the character string names, rather than the array names; 2. Write your subroutine as follows: subroutine foo ( c, type) character *(*) c ! array equivalence integer type ! type of array (real*4, int*2, etc ! **************** parameter maxlength = 200 character*maxlength ctmp real*4 rtmp(maxlength/4) integer*2 itmp(maxlength/2) equivalence( ctmp, rtmp, itmp) ctmp = c if (type .eq. 1) then ! real*4 ... else if (type .eq. 2) then ! integer*2 ... end if end There is, of course, a triple overhead: a string copy, a check on type and wasted space for the dummy character string. An alternative, which I prefer, is to do the local declarations above in a common block and do the string copy in the calling program, bu then, some people are alergic to global variables ... Geoff Croes
BRUCE@UC780.BITNET (05/26/87)
Re: language question C will allow you to cast the array at run time, and vax pascal ( though in my humble opinion the syntax will be messy) will also, though pascal in general won't. bruce (bruce@uc780) on bitnet