From mboxrd@z Thu Jan 1 00:00:00 1970 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: f7344,9a80b8dd0134f87b X-Google-Attributes: gidf7344,public X-Google-Thread: fc89c,9a80b8dd0134f87b X-Google-Attributes: gidfc89c,public X-Google-Thread: 1014db,9a80b8dd0134f87b X-Google-Attributes: gid1014db,public X-Google-Thread: fee84,9a80b8dd0134f87b X-Google-Attributes: gidfee84,public X-Google-Thread: 1094ba,9a80b8dd0134f87b X-Google-Attributes: gid1094ba,public X-Google-Thread: 10b612,9a80b8dd0134f87b X-Google-Attributes: gid10b612,public X-Google-Thread: f678f,9a80b8dd0134f87b X-Google-Attributes: gidf678f,public From: gelato@oort.ap.sissa.it (Sergio Gelato) Subject: Re: C calling Fortran 77 shared library on OSF/1 (or DUnix ?) Date: 1996/05/08 Message-ID: <4mq6jr$1or@ictpsp10.ictp.trieste.it>#1/1 X-Deja-AN: 153727201 references: <4m9s2v$f6u@newton.cc.rl.ac.uk> <4mg704$qbj@ictpsp10.ictp.trieste.it> <4mo2h2$cq1@darkstar.UCSC.EDU> organization: SISSA, Trieste keywords: Fortran 77 common blocks shared library c calling fortran newsgroups: comp.unix.osf.misc,comp.unix.osf.osf1,comp.lang.c,comp.lang.fortran,comp.unix.programmer,comp.unix.ultrix,comp.os.vms In article <4mo2h2$cq1@darkstar.UCSC.EDU>, William Deich wrote: >In article <4mg704$qbj@ictpsp10.ictp.trieste.it>, >Sergio Gelato wrote: >>In article <4m9s2v$f6u@newton.cc.rl.ac.uk>, >>Rachel Kay Street wrote: >>>2) Also any one tried to mix character*(*) array(length) and char *array[] >>> (or char **array) when making calls from C into Fortran 77 functions ? >>I don't know about DEC's Fortran for OSF/1, but that particular mix is >>unlikely to work under other Unix'y implementations. >Actually, that mix will work under most flavors of Unix. Among those No, it's the C side you don't seem to understand. We agree entirely on the Fortran passing convention (the string length is passed as a separate argument). I also agree that the equivalencing rules preclude such things as padding to word boundaries between elements of the array (see below). That means a CHARACTER*(n) array(l) is stored exactly like a char array[l*n] in C. I would have to check the exact wording of the C standard to see if char array[l][n] is guaranteed to be stored in the same way (I already know that it will be in the implementations I use). The essential point here is that char *array[], which is indeed passed to subprograms as if it were char **array, is a different animal: an array of pointers to character variables. These variables are not guaranteed to have the same length, nor to be contiguous in memory, nor to be non-overlapping, and some of the pointers may even be NULL. Therefore, a char ** argument in C is generally not compatible with a Fortran array of character strings. >Unix's with which I am familiar, Fortran compilers under BSD 4.x, >SunOS, Solaris, OSF/1, AIX, and HP-UX, all support the BSD f77 >convention for passing storing and passing [arrays of] character >strings. (HP-UX and AIX have compile-time switches that let you select >between BSD's f77 string-passing convention and another convention.) >The BSD f77 rule is that a pointer to the start of the data is passed >in the argument's normal position in the calling list, and the length >of a character string is attached as an extra argument at the end of >the argument list. >>The equivalent of >>a Fortran CHARACTER array (CHARACTER*(N) array(length)) is usually something >>like char array[length][N], or in some cases char array[length*N]. >>(On machines that are not byte-addressable, I would beware if N is >>not a multiple of the word/byte size ratio.) >I am pretty sure (but I don't have a Fortran manual handy to check on this) >that the rules for Fortran EQUIVALENCE statements between character strings >and character arrays guarantees that this layout holds for all Fortran >implementations. (That is, an array of character strings is laid out in >memory with one character string immediately following the previous.) That certainly needs to be guaranteed in the presence of EQUIVALENCE statements, actual or potential (and all COMMON blocks imply the possibility of such equivalencing). It would be cumbersome for the compiler to generate different code for a routine according to whether the actual argument is equivalenced or not, so we can probably rely on most implementations never introducing any padding between array elements.