[Rcpp-devel] Segfaults

Steve Lianoglou mailinglist.honeypot at gmail.com
Wed Jan 4 15:50:32 CET 2012


Hi,

On Wed, Jan 4, 2012 at 9:33 AM, Hadley Wickham <hadley at rice.edu> wrote:
> Hi all,
>
> Is it a bug to cause a segfault with Rcpp? Or are am I doing something
> so dumb that an automatic check could never protect?  See minimal
> reproducible example below.
>
> Hadley
>
> library(inline)
> f <- cxxfunction(signature(x = "numeric", i = "numeric"), plugin = "Rcpp", '
>  Rcpp::NumericVector x_(x);
>  Rcpp::NumericVector i_(i);
>
>  x_[(int) (i_[0]) - 1] = 1000;
>
>  return(x_);
> ')
> f(1:10, 1) # works
> f(1:10, 1.5) # also works
> f(1:10, NA) # segfaults

I reckon that's because `NA` is actually of `logical` type, where your
first to examples are of the correct (real/double) type.

When I use inline for anything semi-permanent (the last step before
turning it into a package, let's say), I typically have a thin R
wrapper function that calls down to my inline function and I never
call the inline function directly anywhere else. The job of the thin R
wrapper function is to sanity check and/or coerce the vars to the
correct type before they are past to the inline'd function to avoid
the segfault.

-steve

>
> --
> Assistant Professor / Dobelman Family Junior Chair
> Department of Statistics / Rice University
> http://had.co.nz/
> _______________________________________________
> Rcpp-devel mailing list
> Rcpp-devel at lists.r-forge.r-project.org
> https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel



-- 
Steve Lianoglou
Graduate Student: Computational Systems Biology
 | Memorial Sloan-Kettering Cancer Center
 | Weill Medical College of Cornell University
Contact Info: http://cbio.mskcc.org/~lianos/contact


More information about the Rcpp-devel mailing list