Skip to content

Calling Fortran routines

2 messages · Martyn Byng, Duncan Murdoch

#
Hi,

I am having problems calling a fortran routine from within R. When the
routine is called, R exits with an application error:
"The instruction at 0x004a8b7d referenced memory at 0x200000015. The memory
could not be written".

The R code used to call the routine is:
.Fortran("GTEST",a=as.integer(1),b=as.integer(3),c=as.integer(-10),d=as.inte
ger(0),e=as.integer(0))

The Fortran routine itself, is just a dummy routine, created to try and find
out why a more complex routine was causing R to crash, and consists of:

     SUBROUTINE GTEST_(A,B,C,D,E)
	INTEGER A,B,C,D,E

	!DEC$ ATTRIBUTES DLLEXPORT :: GTEST_
	!DEC$ ATTRIBUTES ALIAS:'GTEST_' :: GTEST_

      END

Where the !DEC commands are needed in Visual Fortran to create the DLL (I
think !, these were copied from an example file on the compaq web page).

The strange thing about this is that if I remove variable E from the
subroutine definition and recreate the DLL everything works as expected. The
routine causes R to crash only when it has more than 4 variables.

Any help would be appreciated.

Martyn

PS: I am using R version 1.6.2, under windows 2000 (professional), and the
Fortran code is compiled into a Windows 32bit DLL using Compaq Visual
Fortran Professional, edition 6.1.0

________________________________________________________________________
This e-mail has been scanned for all viruses by Star Internet. The
service is powered by MessageLabs. For more information on a proactive
anti-virus service working around the clock, around the globe, visit:
http://www.star.net.uk
1 day later
#
On Thu, 3 Apr 2003 11:14:55 +0100 , you wrote in message
<A1B373D68745D31180D100A0244BB400019F2FDF at nagmail.nag.co.uk>:
I don't know that compiler at all, but the usual cause of crashes like
this is using incompatible calling conventions.  The usual calling
convention for a DLL in Windows is "stdcall", but R uses "cdecl";
since I didn't see anything mentioned in your source, I think you were
probably using "stdcall".

Hopefully your documentation will tell you how to ask for "cdecl".

Once you find this, could you please email me a short paragraph saying
in detail what to do with your compiler?  I'm putting together a web
page showing what's necessary in various cases.   My current
incomplete draft is visible here: 

 <http://www.stats.uwo.ca/faculty/murdoch/software/compilingDLLs>

Duncan Murdoch