Message-ID: <CADNH-PvAqMd5J88vKMTjYoMptGuDx97WEvDs_zqf9w_Ad2N87A@mail.gmail.com>
Date: 2011-08-04T11:31:17Z
From: Christian Gunning
Subject: [Rcpp-devel] Pointer troubles
In-Reply-To: <CADNH-PstNavd7CMB=R4eBc-8O15sj6GiAX+BKC02gXrPLiLecg@mail.gmail.com>
Q: In latex code after quote, I've highlighted the new/free/finalizer
issue (adapted from Romain). Alternately, this code (again, Romain's)
highlights passing an SEXP to XPtr<...>. It seems that one or the
other should be primary.
SEXP doubleIntPointer(SEXP test){
XPtr<int> test2(test) ;
return wrap( *test2 * 2 ) ;
}
Comment: A concise example that uses an XPtr in a C++ function is
still lacking. I'll look at this now+20hrs-ish.
It's been a hectic summer, sorry to reappear on list out of nowhere...
-xian
> The only hard work I'm doing is reading and understanding --
> other than that it's just copy editing :)
> I'm going to start with something *waaaaaay* simpler (and shorter),
> just to walk through concepts. Let me squint some more. I'll aim
> post a ~30 line draft in the next 12 hours or so.
>
> -xian
>
> --
> A man, a plan, a cat, a ham, a yak, a yam, a hat, a canal ? Panama!
>
\paragraph{External Pointers}~
\newline
<<lang=cpp>>=
// The Rcpp::XPtr template class is parameterized by the class of the
pointer. XPtr objects are smart pointers and can be passed to C++
functions that require pointers. XPtr objects can also be wrap()ed
and passed back to R as external pointers.
// XPtr includes a default finalizer. Thus in this example test need
not be freed.
SEXP getIntPointer(){
int *test = new int;
*test = 6;
XPtr<int> retVal(test);
return retVal ;
}
// Define a simple function
double sumFun(SEXP xs) {
Rcpp::NumericVector x(xs);
double sum = std::accumulate(x.begin(), x.end(), 0.0);
return(sum);
}
// Define a pointer to the above function
typedef double (*funcPtr)(SEXP);
ret = XPtr<funcPtr>(new funcPtr(&sumFun));
return ret;
@