Problems with assigning large vectors

i have the following program.

#include <iostream>
#include <vector>
#include <algorithm>
#include <cassert>
#include <ctime>
#include <cmath>
#include <RInside.h>
using std::cout;
using std::endl;
using std::vector;
using namespace Rcpp;
int main(int argc, char** argv){
    RInside R;
    R.parseEvalQ("set.seed(1)"); Rcpp::RNGScope();
    const Function sample("sample");
    vector<int> vv = as<vector<int> >(sample(NumericVector::create(0,1,2,3), 1e6, true,NumericVector::create(.3,.3,.2,.2)));
    cout<<"1"<<endl<<std::flush;
    const vector<int> vv2 = as<vector<int> >(sample(NumericVector::create(0,1,2,3), 2e6, true,NumericVector::create(.3,.3,.2,.2)));
    cout<<"2"<<endl;
}

The output is

1
Segmentation fault

That means the C++ vector vv2 cannot be initialized. Why can't vv2 be assigned?

With inline, here is what i got:

> body <- '
+ using namespace Rcpp;
+ using std::vector;
+ using std::cout;
+ using std::endl;
+      Rcpp::RNGScope();
+     const Function sample("sample");
+     vector<int> vv = as<vector<int> >(sample(NumericVector::create(0,1,2,3), 1e6, true,NumericVector::create(.3,.3,.2,.2)));
+     cout<<"1"<<endl<<std::flush;
+     const vector<int> vv2 = as<vector<int> >(sample(NumericVector::create(0,1,2,3), 2e6, true,NumericVector::create(.3,.3,.2,.2)));
+     cout<<"2"<<endl;
+     List vecs(2);
+     vecs[1]=vv;
+     vecs[1]=vv2;
+     return(vecs);
+     '
> require( inline )
Loading required package: inline
> require( Rcpp )
Loading required package: Rcpp
Loading required package: int64
> 
> signatures <- NULL
> fx <- cxxfunction( signatures, body, plugin = "Rcpp" )
> a <- fx()

 *** caught segfault ***
address 0x7f2b850eb013, cause 'memory not mapped'

Traceback:
 1: .Primitive(".Call")(<pointer: 0x7f2b8627d2c0>)
 2: fx()

Possible actions:
1: abort (with core dump, if enabled)
2: normal R exit
3: exit R without saving workspace
4: exit R saving workspace

Answers


Can you do the same in R on your machine? 2 million elements is quite a lot.

Also, I almost never call back to R functions this way as it is inefficient. If you want speed-up, reimplement sample() in C++ which can't be too hard.

Next, I'd also recommend simpler expression first tried via inline.

Lastly, and I'll sound like a broken record, try the rcpp-devel list.


Need Your Help

NSDictionary Key For Value/Object?

objective-c cocoa-touch ios nsdictionary

Can we get the key for an object in an NSDictionary by passing a particular value or object?

onServiceConnected not being called in bound service

android service

I am testing out the sample code provided by Google for a bound service. This is the sevice:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.