[Rcpp-devel] Segfaults when declaring Vector types

Dirk Eddelbuettel edd at debian.org
Thu Apr 14 15:03:12 CEST 2011


Hi Patrick,

Welcome!

On 14 April 2011 at 18:27, Patrick Ye wrote:
| Hi all,
| 
| I'm completely new to R and Rcpp, and I thought I'd just make a simple program
| to see how Rcpp works. Here is the code:

You are a tad confused, which is excuseable given the amount of
documentation.  You wrote a self-contained C++ program (a good thing in and
by itself :-) to which you tried to attach an Rcpp object.  Completely
outside of R.

That makes no sense whatsoever.  Think of Rcpp as a somewhat smoother wrapper
around the sometimes edge C API for R.  There is no use case for Rcpp if you
do not have R.

Maybe you misunderstood and you wanted to attach R to a C++ program---for
that we have RInside. Install it from CRAN, look at dozens of examples in the
package.  RInside benefits greatly from Rcpp and uses it, so what you learned
can be applied there.

Or maybe you want to attach C++ functions to R after all. In which case you
may want to look at inside (also on CRAN) and the example posted here. You
would never write a main() function --- as R's rules the roost.

Hope this helps, Dirk

PS  Use std::vector instead of a double*, then it doesn't matter that you
    forgot to free the object you allocated :)

PPS One way to compile your program is 

    g++ -o py py.cpp `r -e'Rcpp:::CxxFlags()'` `R CMD config --cppflags` \
                     `R CMD config --ldflags` `r -e'Rcpp:::LdFlags()'`

    where Rscript could be used instead of r.  And yes the seg.fault is
    confirmed, but this is simply no use case


| 
| <code>
| #include <Rcpp.h>
| #include <cstdio>
| #include <cstdlib>
| #include <iostream>
| RcppExport void get_weights(int num_of_numbers, double* numbers);
| RcppExport void get_weights(int num_of_numbers, double* numbers)
| {
|     std::cerr << "--------" << num_of_numbers << "--------------\n";
|     Rcpp::NumericVector vec(1);
| }
| int main(int argc, char* argv[])
| {
|     int num_of_numbers = argc - 1;
|     double* numbers = new double[num_of_numbers];
|     for(int i = 1; i < argc; ++i)
|     {
|         numbers[i-1] = atof(argv[i]);
|         std::cerr << numbers[i-1] << "\n";
|     }
|     get_weights(num_of_numbers, numbers);
|     return 0;
| }
| </code>
| 
| I can compile and link this code without any problems. However, when I run it,
| I get a segfault in
| get_weights. I traced the segfault using gdb, and the problem sees to come from
| this:
| 
| <code>
| (gdb) run  1 2 3 4 5 6 7 1 2 3 4 5 6 7 1 2 3 4 5 6 7
| Starting program: /home/patricky/tmp/stl 1 2 3 4 5 6 7 1 2 3 4 5 6 7 1 2 3 4 5
| 6 7
| [Thread debugging using libthread_db enabled]
| 1
| 2
| 3
| 4
| 5
| 6
| 7
| 1
| 2
| 3
| 4
| 5
| 6
| 7
| 1
| 2
| 3
| 4
| 5
| 6
| 7
| --------21--------------
| 
| Program received signal SIGSEGV, Segmentation fault.
| 0x00007ffff6c3ed70 in ?? () from /usr/lib64/R/lib/libR.so
| (gdb) up
| #1  0x00007ffff6c4049b in Rf_allocVector () from /usr/lib64/R/lib/libR.so
| (gdb)
| #2  0x0000000000401814 in Vector (this=0x7fffffffdf80, size=@0x7fffffffdf9c)
| at /usr/share/lib/R/x86_64-pc-linux-gnu-library/2.12/Rcpp/include/Rcpp/vector/
| Vector.h:130
| 130             RObject::setSEXP( Rf_allocVector( RTYPE, size) ) ;
| </code>
| 
| I've rather stumped at this problem, and would appreciate any help that you
| guys can provide.
| 
| Thanks a lot.
| 
| Patrick
| 
| ----------------------------------------------------------------------
| _______________________________________________
| 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

-- 
Dirk Eddelbuettel | edd at debian.org | http://dirk.eddelbuettel.com


More information about the Rcpp-devel mailing list