🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.sys.dec
Article #5001

Re: Problem calling a C function from Fortran

#5001
From: Larry Jones
Date: Sun, 21 Aug 1988 16:35
55 lines
3317 bytes
In article <1134@ssc-bee.ssc-vax.UUCP>, okinaka@ssc-vax.UUCP (Nathan Okinaka) writes:
< I'd appreciate if anybody out there could help me with a problem I'm having 
< calling a function written in C from a Fortran program on the VAX/VMS.

Well, the problem is actually calling a FORTRAN subroutine from C, but read on.
 
< I'm passing two arguments to a C function called "test".  The first one is 
< an integer*4 varaible and the second one is an external function.  The first 
< argument gets passed ok but the external function "dbl" does not seem to get 
< passed ok.

Sure it does, you just called it wrong.
 
< The C function requires the address of where the function "dbl" starts so
< I'm passing the value of the address of the function "dbl" to the C function
< "test" but it does not seem to work.  I'm wondering what Fortran does when you
< try and get %loc(dbl).  Is this really the address of where this function 
< starts?  What am I doing wrong?  Any help would be appreciatted.  This is a
< test program I'm trying out which I need to use in another bigger program.
< 
< Thanks,
< Nate Okinaka (206)773-2687
< 
< The following is the error message I get when I run the program:
< -----------------------------------------------------------------------------
< [okinaka]> r ext
< %loc(dbl) =        4848
< %loc(dbl) in hex =    12F0
< iangle =          90
< iangle in hex =      5A
< This is a test of external function
< x = 90, f = 4848 (decial)
< x = 5a, f = 12f0 (hex)
< %SYSTEM-F-ACCVIO, access violation, reason mask=00, virtual address=0000005A, 
< PC=000012F9, PSL=03C00020
< %TRACE-F-TRACEBACK, symbolic stack dump follows
< module name     routine name                     line       rel PC    abs PC
< DBL             DBL                                 5      00000009  000012F9
< TEST            test                                9      00000039  000013AD
< EXT$MAIN        EXT$MAIN                           17      000000AA  000012AA

Well, the traceback shows that your FORTRAN main program did call your C
function which then called the FORTRAN subroutine so that's not the problem.
The problem is that when the C function calls the FORTRAN subroutine, it passes
the VALUE of the argument instead of the ADDRESS.  When the FORTRAN subroutine
tried to access the variable, it used the value as an address and tried to
get the value stored there and that's what caused the access violation.
Just pass the address instead and everything will work fine.

----
Larry Jones                         UUCP: uunet!sdrc!scjones
SDRC                                      scjones@sdrc
2000 Eastman Dr.                    BIX:  ltl
Milford, OH  45150                  AT&T: (513) 576-2070
Nancy Reagan on superconductivity: "Just say mho."
Technical Details
Message-ID: <353@sdrc.UUCP>
Path: arc1-ca.pugleaf.net!utzoo!utgpu!water!watmath!clyde!att!alberta!ubc-cs!uw-beaver!cornell!batcomputer!itsgw!steinmetz!uunet!sdrc!scjones
References: <1134@ssc-bee.ssc-vax.UUCP>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!water!watmath!clyde!att!alberta!ubc-cs!uw-beaver!cornell!batcomputer!itsgw!steinmetz!uunet!sdrc!scjones
From: scjones@sdrc.UUCP (Larry Jones)
Newsgroups: comp.sys.dec,comp.lang.fortran,comp.lang.c
Subject: Re: Problem calling a C function from Fortran
Keywords: Fortran, C, external function in Fortran
Message-ID: <353@sdrc.UUCP>
Date: 21 Aug 88 16:35:27 GMT
References: <1134@ssc-bee.ssc-vax.UUCP>
Organization: Structural Dynamics Research Corp., Cincinnati
Lines: 55