From p.dalgaard at biostat.ku.dk  Sun Sep  4 21:54:20 2005
From: p.dalgaard at biostat.ku.dk (Peter Dalgaard)
Date: 04 Sep 2005 21:54:20 +0200
Subject: [Rd] .Call and Segmentation Fault
In-Reply-To: <973080e105090412324891faa6@mail.gmail.com>
References: <355C35514FEAC9458F75947F5270974D076CA3@usctmx1103.merck.com>
	<973080e105090412324891faa6@mail.gmail.com>
Message-ID: <x27jdwlic3.fsf@turmalin.kubism.ku.dk>

Ricardo Luiz Andrade Abrantes <rabrantes82 at gmail.com> writes:

> Hello there!
> I almost don't deal with SEXPs. The function's name is main() it returns a 
> SEXP wich is R_NilValue, thats all I use of SEXPs. This function call an 
> optimization packge and the output goes all to a text file.
> Well, I followed Mr. Lumley's hint and used gdb with R and my program. 
> Together with a friend we could find the problem.
> Inside the Fortran optimization package there's a subroutine called cg(...), 
> and for some wierd reason this subroutine is never called (so, some 
> variables are not correctly initialized) and the program becomes crazy.
> I did this small example to show you what happens:
> 
> File: prog.f
> -------------
> subroutine cg()
> write(*,*) 'Just a simple test'
> end
> 
> subroutine program()
> write(*,*) 'Calling the function...'
> call cg()
> end
> 
> File test.h
> ------------
> #include "cfortran.h"
> 
> PROTOCCALLSFSUB0(PROGRAM,program)
> #define program() CCALLSFSUB0(PROGRAM,program)
> 
> 
> File test.c
> ------------
> #include <R.h>
> #include <Rdefines.h>
> #include <stdio.h>
> #include "test.h"
> 
> 
> SEXP simple_program(){
> program();
> return R_NilValue;
> }
> 
> 
> I compile the C and Fortran souces into a shared lib. I open R, do a 
> dyn.load("lib's name") and then a .Call("simple_program").
> What I got?
> Calling the function...
> Segmentation fault
> 
> What if I change the cg function's name to pp? My Fortran code is now:
> 
> File: prog.f
> -------------
> subroutine pp()
> write(*,*) 'Just a simple test'
> end
> 
> subroutine program()
> write(*,*) 'Calling the function...'
> call pp()
> end
> 
> And the output from R is:
> 
> Calling the function...
> Just a simple test
> NULL
> 
> Can anyone explain this? Is there a way to solve it? The optimization 
> package I use has the "cg" function, and I cannot change it's name!

Hmmmm.... Did you ever tell us

1) Exactly what your platform is
2) How you generated the shared library (R CMD SHLIB, or?)
3) What you did to load it into R

?

-- 
   O__  ---- Peter Dalgaard             ?ster Farimagsgade 5, Entr.B
  c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
 (*) \(*) -- University of Copenhagen   Denmark          Ph:  (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)                  FAX: (+45) 35327907



[Rd] .Call and Segmentation Fault
Peter Dalgaard