From sdavis2 at mail.nih.gov  Tue Jan 10 20:18:18 2006
From: sdavis2 at mail.nih.gov (Sean Davis)
Date: Tue, 10 Jan 2006 14:18:18 -0500
Subject: [Rd] Issue with c++ .C call
In-Reply-To: <43C3FFEB.3010506@DecisionSynergy.com>
Message-ID: <BFE972AA.3677%sdavis2@mail.nih.gov>




On 1/10/06 1:41 PM, "Dominick Samperi" <dsamperi at DecisionSynergy.com> wrote:

> Sean,
> 
> prm in your function calcStepgram is NOT a vector of doubles, it is of type
> SEXP, and you need to use R macros to fetch the value(s). This is done
> automatically in the Rcpp package, and if you want to see how this is
> done look at the definition of the class RcppVector in Rcpp.cpp

I'm not sure why the prm parameter is not a vector; I thought this was the
standard way to create a vector of doubles....  So to confuse myself even
more, I have added a simple C function that looks simply calls my C++
function like this:

extern "C" {
  void mymain(double *data,double *prm, double* intervals, int* max, int *n,
double *plot) {
    calcStepgram(data,prm,intervals,max,n,plot);
  }
}

Then, I call this function using .C('mymain',...) as before.  Note the
change in parameter memory locations from the call to mymain and the call to
calcStepgram.  Why does this happen?

> dyn.load('StepGram/src/Stepgram.so')
> mySG <- function(dat1,thresh,noise) {
  vec <- c(thresh,noise)
  .C('mymain',
     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)

....

(gdb) break mymain
Breakpoint 1 at 0x10c13f0: file Stepgram.cpp, line 23.
(gdb) c
Continuing.
> mySG(dat1=dat,thresh=3,noise=1)

Breakpoint 1, mymain (data=0x1137148, prm=0x195cd30, intervals=0x1386018,
max=0x1b208e0, n=0x1b20900, plot=0x1137208) at Stepgram.cpp:23
23          calcStepgram(data,prm,intervals,max,n,plot);
(gdb) print prm[0]
$1 = 3
Current language:  auto; currently c++
(gdb) print prm[1]
$2 = 1
(gdb) print max[0]
$3 = 10000
(gdb) s       
calcStepgram (data=0x1137148, prm=0x180da24, intervals=0x195cd68,
max=0x1999d6c, n=0xbfffd6e0, plot=0x0) at Stepgram.cpp:29
29          Stepgram* sg=new Stepgram(data, *n);
(gdb) print prm[0]
$4 = 2.0002441518028817
(gdb)



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