From sdavis2 at mail.nih.gov  Tue Jan 10 19:13:52 2006
From: sdavis2 at mail.nih.gov (Sean Davis)
Date: Tue, 10 Jan 2006 13:13:52 -0500
Subject: [Rd] Issue with c++ .C call
Message-ID: <BFE96390.3659%sdavis2@mail.nih.gov>

I am still having some difficulties with connecting R to a C++ function.  I
am able to call the function as expected after compiling the shared library
and such.  However, the call to the function is via .C; parameters from the
.C call are not being passed correctly to the function.  As an example, I
have attached a GDB run of the code.  I set a breakpoint on entry to the
function I am calling from R.  What is bothering me (and probably causing
the segmentation faults I am seeing) is that the parameter
prm=as.double(c(3.2,1.1)) is not 3.2,1.1 IMMEDIATELY after the call to .C.
I am sure I am missing something very basic.

Thanks,
Sean


> sessionInfo()
R version 2.2.0, 2005-08-11, powerpc-apple-darwin7.9.0

attached base packages:
[1] "methods"   "stats"     "graphics"  "grDevices" "utils"     "datasets"
[7] "base"     




holmes:~/Mercury/projects/R/StepGram sdavis$ R -d gdb
GNU gdb 6.1-20040303 (Apple version gdb-413) (Wed May 18 10:17:02 GMT 2005)
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain
conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for details.
This GDB was configured as "powerpc-apple-darwin"...Reading symbols for
shared libraries ... done

(gdb) r
Starting program: 
/Users/sdavis/R-devel2/R.framework/Versions/2.2.0/Resources/bin/exec/R
Reading symbols for shared libraries ...........+ done

R : Copyright 2005, The R Foundation for Statistical Computing
Version 2.2.0 Under development (unstable) (2005-08-11 r35256)
ISBN 3-900051-07-0

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for a HTML browser interface to help.
Type 'q()' to quit R.

Reading symbols for shared libraries
............................................................. done
Reading symbols for shared libraries . done
Reading symbols for shared libraries . done
> dyn.load('StepGram/src/Stepgram.so')
Reading symbols for shared libraries .. done
> mySG <- function(dat1,thresh,noise) {
  vec <- c(thresh,noise)
  .C('calcStepgram',
     data=as.double(dat1),
     prm=as.double(vec),
     intervals=double(10000*3+1),
     max=as.integer(10000),
     n=as.integer(length(dat1)),
     plot=double(length(dat1)))}
> dat <- c(0.01,0.1,-0.2, 0.1,-0.1,-1000,3.2,3.5,-1.3,3.1,
3.2,3.1,-0.1,0.2,0.15,-0.05,-0.1,0.2,0.1,-0.1)
> 
Program received signal SIGINT, Interrupt.
0x9001f208 in select ()
(gdb) break calcStepgram
Breakpoint 1 at 0x10bb418: file Stepgram.cpp, line 22.
(gdb) c
Continuing.
> mySG(dat1=dat,thresh=3.2,noise=1.1)

Breakpoint 1, calcStepgram (data=0x1137048, prm=0x1c81eb0,
intervals=0xbfffd7e0, max=0x2954840, n=0xbfffd6c0, plot=0x180d574) at
Stepgram.cpp:22
(gdb) print prm[0]
$1 = 1.7716149411915527e-303    <<<<<------This should be 3.2!
Current language:  auto; currently c++



[Rd] Issue with c++ .C call
Sean Davis