[Bug fortran/45466] New: Bus Error: C program calls Fortran Function which has returned value as Character string
The program will crash if compile with version 4.4.3 or 4.3.2 but works with 4.1.2. Main program is written in C. (see the following) /* * C file passdouble.c * To compile the program, using the following command. *gcc passdouble.c requestdouble.o -lgfortran */ #include extern char* requestdouble_(double*,double*); int main() { double lat=10.0; double lon=20.0; requestdouble_(&lat,&lon); return 0; } The Fortran function is in the file requestdouble.f90 shown below. ! gfortran -c -g -Wall requestdouble.f90 FUNCTION requestdouble(rlat,rlng) ! IMPLICIT NONE ! ! Arguments ! ! Input scalars REAL(KIND=8), INTENT(IN) :: rlat ! - latitude - REAL(KIND=8), INTENT(IN) :: rlng ! - longitude - ! CHARACTER(LEN=16) :: requestdouble ! PRINT *, ' requestdouble rlat=',rlat,' rlng=',rlng requestdouble='' RETURN END FUNCTION requestdouble Here is how it compiled and ran. gfortran -c -g -Wall requestdouble.f90 gcc passdouble.c requestdouble.o -lgfortran a.out Bus error -- Summary: Bus Error: C program calls Fortran Function which has returned value as Character string Product: gcc Version: 4.4.3 Status: UNCONFIRMED Severity: normal Priority: P3 Component: fortran AssignedTo: unassigned at gcc dot gnu dot org ReportedBy: Lulin dot Song at gmail dot com http://gcc.gnu.org/bugzilla/show_bug.cgi?id=45466
[Bug fortran/45466] Bus Error: C program calls Fortran Function which has returned value as Character string
--- Comment #4 from Lulin dot Song at gmail dot com 2010-08-31 18:02 --- (In reply to comment #1) > I think the return value for character(16) returns are passed via the first > argument. So I think this is invalid. > If the return value of function 'requestdouble' is changed to be integer or double, the program will work. Do you mean that only if the return value is character string, then it will be passed back through first argument (at '&rlat' posistion)? Confused.(In reply to comment #1) > I think the return value for character(16) returns are passed via the first > argument. So I think this is invalid. > If the return value of function 'requestdouble' is changed to be integer or double, the program will work. Do you mean that only if the return value is character string, then it will be passed back through first argument (at '&rlat' posistion)? Confused. -- http://gcc.gnu.org/bugzilla/show_bug.cgi?id=45466
[Bug fortran/45466] Bus Error: C program calls Fortran Function which has returned value as Character string
--- Comment #5 from Lulin dot Song at gmail dot com 2010-08-31 18:06 --- (In reply to comment #2) > Try compiling with -fdump-tree-original and inspecting the > expected argument lists. You really don't want to use a > function here. Use a subroutine. > > include > > void requestdouble_(double*, double*, char *, int *len); > > int main() > { > char str[20]; > int len; > double lat=10.0; > double lon=20.0; > requestdouble_(&lat, &lon, str, &len); > return 0; > } > subroutine requestdouble(rlat,rlng,str) > IMPLICIT NONE > REAL(KIND=8), INTENT(IN) :: rlat ! - latitude - > REAL(KIND=8), INTENT(IN) :: rlng ! - longitude - > CHARACTER(LEN=*) :: str > PRINT *, ' requestdouble rlat=', rlat,' rlng=', rlng > str='' > RETURN > END subroutine requestdouble > Thanks. I do know how to work around it with subroutine which I already did in my program. But it doesn't explain why 4.1.2 version allows return character string from function. Our program works well until the gcc upgrade. Is this new standard? -- http://gcc.gnu.org/bugzilla/show_bug.cgi?id=45466
[Bug fortran/45466] Bus Error: C program calls Fortran Function which has returned value as Character string
--- Comment #9 from Lulin dot Song at gmail dot com 2010-09-01 14:22 --- (In reply to comment #8) > (In reply to comment #0) > > Main program is written in C. (see the following) > > I strongly suggest using the C Binding facility of Fortran 2003 instead of > relying on the internals of a given compiler. GCC/gfortran supports the C > binding since 4.3.0 and newer version of most other compilers support it as > well. > > Cf. http://gcc.gnu.org/onlinedocs/gfortran/Interoperability-with-C.html > > The article at > http://www.fortranplus.co.uk/resource/fortran_2003_2008_compiler_support.pdf > lists for some compilers the support of Fortran 2003 (and 2008) features; > interesting for you are the items "Interoperability with C". Note: The list is > incomplete as not all vendors have submitted their data. Thank you all for the information and suggestions. Fortran2003 C Binding facility seems to be what we should do next. Our project is a large Fortran77,90,95 C mixing language software been developed for years. It is hard to make it work with three different compilers (Intel ifort, AbfSalford ftn95 and gfortran). > > Regarding GCC/gfortran 4.1: I have no idea why it worked for you, but the > calling convention of 4.1 was the same (I just checked). > It is mystery to me too. The small program I submitted here works if compiled with 4.1.2 . Another curious thought. When the return value is integer or double, the dump looks like requestdouble (real(kind=8) & rlat, real(kind=8) & rlng) when the return value is character string, is it more intuitive to be requestdouble ( real(kind=8) & rlat, real(kind=8) & rlng,character(kind=1)[1:16] & __result, integer(kind=4) .__result) instead of requestdouble (character(kind=1)[1:16] & __result, integer(kind=4) .__result , real(kind=8) & rlat, real(kind=8) & rlng) ? I mean the ".__result" appending to arguments list instead inserting before arguments list. Many thanks again. -- http://gcc.gnu.org/bugzilla/show_bug.cgi?id=45466
[Bug fortran/45466] Bus Error: C program calls Fortran Function which has returned value as Character string
--- Comment #11 from Lulin dot Song at gmail dot com 2010-09-01 15:29 --- (In reply to comment #10) > (In reply to comment #9) > > when the return value is character string, is it more intuitive to be > > > > requestdouble ( real(kind=8) & rlat, real(kind=8) & > > rlng,character(kind=1)[1:16] & __result, integer(kind=4) .__result) > > > > instead of > > > > requestdouble (character(kind=1)[1:16] & __result, integer(kind=4) > > .__result > > , real(kind=8) & rlat, real(kind=8) & rlng) ? > > I find both equally intuitive. I think the current order matches g77 and some > other compilers; for instance NAG uses: > void > requestdouble_(requestdouble_Result,requestdouble_ResultLen,rlat_,rlng_) > > Thus as the other order is not clearly superior, sticking to the current order > avoids a lot of trouble both for the compiler development and for user code. > > > > with three different compilers (Intel ifort, AbfSalford ftn95 and gfortran). > > Ifort supports Fortran 2003's C binding since quite some time; I think Salfort > doesn't nor does Absoft, but I might be wrong. (Of AbfSalfort I have never > heard.) > AbfSalfort is typo. We use Salfort ftn95 on Windows. > For general Fortran discussion, I suggest the newsgroup comp.lang.fortran (via > your news server or via http://groups.google.com/group/comp.lang.fortran/) or > the gfortran mailing list. > > A bug report is not the proper method of communication :-) > Thanks a lot for your explaining. I thought it was bug since it behaves different when the return type is different. I will submit future report to mail list first to see anyone can answer there to avoid flooding the bugzilla. -- Lulin dot Song at gmail dot com changed: What|Removed |Added Status|RESOLVED|VERIFIED http://gcc.gnu.org/bugzilla/show_bug.cgi?id=45466