I am tring to use Rcpp Module to export C++ class to R, but meet some problem.<div>Here is my code:</div><div>###########################################################################################</div><div>/* file PL modules.cpp */</div>
<div><div>using namespace Rcpp;</div><div>using namespace std;</div><div><br></div><div>class SymPL {</div><div>public:</div><div> SymPL(vector<double> close);</div><div> void update(int idx, </div><div><span class="Apple-tab-span" style="white-space:pre"> </span> double txnqty, </div>
<div><span class="Apple-tab-span" style="white-space:pre"> </span> double txnprc);</div><div> DataFrame toDF();</div><div> </div><div><br></div><div> int nrow;</div><div> vector<double> txn_qty, txn_prc, txn_fee;</div>
<div> vector<double> pos_qty, close_prc, PL;</div><div>};</div><div><br></div><div>SymPL::SymPL(vector<double> close) {</div><div> nrow = close.size();</div><div> txn_qty = vector<double>(nrow);</div>
<div> txn_prc = vector<double>(nrow);</div><div> txn_fee = vector<double>(nrow);</div><div> pos_qty = vector<double>(nrow);</div><div> close_prc = vector<double>(close);</div><div> PL = vector<double>(nrow);</div>
<div>}</div><div><br></div><div>void SymPL::update(int idx,</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> double txnqty, </div><div><span class="Apple-tab-span" style="white-space:pre"> </span> double txnprc) {</div>
<div> txn_qty[idx] = txnqty;</div><div> txn_prc[idx] = txnprc;</div><div> txn_fee[idx] = 0; //calfee(txnqty*txnprc); todo: write a function to cal fee</div><div> if(idx == 0) { //index start with 0</div><div> pos_qty[idx] = txnqty;</div>
<div> PL[idx] = txnqty * (close_prc[idx] = txnprc) - txn_fee[idx];</div><div> }</div><div> else {</div><div> pos_qty[idx] = txnqty + pos_qty[idx-1];</div><div> PL[idx] = pos_qty[idx-1] * (close_prc[idx] - close_prc[idx-1]) + </div>
<div> txnqty * (close_prc[idx] = txnprc) - txn_fee[idx];</div><div> }</div><div>}</div><div><br></div><div>DataFrame SymPL::toDF() {</div><div> DataFrame PLrecord = DataFrame::create(_["txn.qty"] = txn_qty,</div>
<div><span class="Apple-tab-span" style="white-space:pre"> </span> _["txn.prc"] = txn_prc,</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> _["txn.fee"] = txn_fee,</div><div>
<span class="Apple-tab-span" style="white-space:pre"> </span> _["pos.qty"] = pos_qty,</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> _["close.prc"] = close_prc,</div><div>
<span class="Apple-tab-span" style="white-space:pre"> </span> _["PL"] = PL);</div><div> return PLrecord;</div><div>}</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> </div><div>class PortPL {</div>
<div>public:</div><div> CharacterVector symnames;</div><div> int sym_num;</div><div> double initMoney;</div><div> int period_num;</div><div> vector<double> total_eq;</div><div> vector<double> total_PL;</div>
<div> vector<double> period_return;</div><div> vector<double> cum_return;</div><div> DatetimeVector timeindex;</div><div> </div><div> PortPL(SEXP times,</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> SEXP CLprc, double initM);</div>
<div> void update(int idx, NumericVector txnqty_vec, </div><div><span class="Apple-tab-span" style="white-space:pre"> </span> NumericVector txnprc_vec);</div><div> void exit(int idx, NumericVector txnprc_vec);</div>
<div> void hold(int idx);</div><div> NumericVector getpos(int idx);</div><div> List getSymPLs();</div><div><br></div><div>private:</div><div> vector<SymPL> symbols;</div><div><br></div><div>};</div><div><br></div>
<div>PortPL::PortPL(SEXP times,</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> SEXP CL, </div><div><span class="Apple-tab-span" style="white-space:pre"> </span> double initM):</div><div>
timeindex(times) {</div><div> List CLprc(CL);</div><div> sym_num = CLprc.size();</div><div> symnames = CLprc.names();</div><div> initMoney = initM;</div><div> //timeindex = DatetimeVector(times);</div><div> period_num = timeindex.size();</div>
<div> </div><div> //initialize total_eq, total_PL, period_return, cum_return</div><div> total_eq = vector<double>(period_num);</div><div> total_PL = vector<double>(period_num);</div><div> period_return = vector<double>(period_num);</div>
<div> cum_return = vector<double>(period_num);</div><div> </div><div> //initialize each SymPL</div><div> for(int i = 0; i < sym_num; i++) {</div><div> vector<double> close = as<vector<double> >(CLprc[i]);</div>
<div> symbols[i] = SymPL(close);</div><div> }</div><div>}</div><div><br></div><div>void PortPL::update(int idx, NumericVector txnqty_vec, </div><div><span class="Apple-tab-span" style="white-space:pre"> </span> NumericVector txnprc_vec) {</div>
<div> double sumPL = 0;</div><div> for(int i = 0; i < sym_num; i++) {</div><div> symbols[i].update(idx, txnqty_vec[i], txnprc_vec[i]);</div><div> sumPL += symbols[i].PL[idx];</div><div> }</div><div> </div><div>
total_PL[idx] = sumPL;</div><div> if(idx == 0) {// index starts with 0</div><div> total_eq[idx] = initMoney + total_PL[idx];</div><div> period_return[idx] = total_PL[idx] / initMoney;</div><div> cum_return[idx] = total_PL[idx] / initMoney;</div>
<div> }</div><div> else {</div><div> total_eq[idx] = total_eq[idx-1] + total_PL[idx];</div><div> period_return[idx] = total_PL[idx] / total_eq[idx];</div><div> cum_return[idx] = total_eq[idx] / initMoney - 1;</div>
<div> }</div><div>}</div><div><br></div><div>void PortPL::hold(int idx) {</div><div> NumericVector tempv(sym_num, 0.0);</div><div> this->update(idx, tempv, tempv);</div><div>}</div><div><br></div><div>void PortPL::exit(int idx, NumericVector txnprc_vec) {</div>
<div> NumericVector curr_pos(sym_num);</div><div> for(int i = 0; i < sym_num; i++) {</div><div> curr_pos[i] = symbols[i].pos_qty[idx-1];</div><div> }</div><div> this->update(idx, curr_pos, txnprc_vec);</div><div>
}</div><div><br></div><div>NumericVector PortPL::getpos(int idx) {</div><div> NumericVector curr_pos(sym_num);</div><div> for(int i = 0; i < sym_num; i++) {</div><div> curr_pos[i] = symbols[i].pos_qty[idx-1];</div>
<div> }</div><div> return curr_pos;</div><div>}</div><div><br></div><div>List PortPL::getSymPLs() {</div><div> List allsym(sym_num);</div><div> for(int i = 0; i < sym_num; i++){</div><div> allsym[i] = symbols[i].toDF();</div>
<div> }</div><div> allsym.names() = symnames;</div><div> return allsym;</div><div>}</div><div><br></div><div>RCPP_MODULE(PortPL_module) {</div><div> class_<PortPL>("PortPL")</div><div> .constructor<SEXP, SEXP, double>()</div>
<div> .field("total.eq", &PortPL::total_eq)</div><div> .field("total.PL", &PortPL::total_PL)</div><div> .field("symnames", &PortPL::symnames)</div><div> .field("cum.return", &PortPL::cum_return)</div>
<div> .field("period.return", &PortPL::period_return)</div><div> .field("initMoney", &PortPL::initMoney)</div><div> </div><div> .method("update", &PortPL::update)</div>
<div> .method("exit", &PortPL::exit)</div><div> .method("hold", &PortPL::hold)</div><div> .method("getpos", &PortPL::getpos)</div><div> .method("getSymPLs", &PortPL::getSymPLs)</div>
<div> ;</div><div>}</div></div><div><br></div><div>#######################################################################</div><div><br></div><div>## R code</div><div><div>library(inline)</div><div>library(Rcpp)</div>
<div><br></div><div>fx <- cxxfunction(signature(), </div><div> includes = paste(readLines("PnL module.cpp"),</div><div> collapse = "\n"),</div><div>
plugin = "Rcpp")</div><div><br></div><div>port <- Module("PortPL_module", getDynLib(fx)) </div><div>PortPL <- port$PortPL</div><div><br></div><div>datel <- as.POSIXct(Sys.Date()+1:10)</div>
<div>colse <- 1:10</div><div>initM <- 1e5</div><div><br></div><div>testport <- new(PortPL, datel, list(hjs=colse), initM)</div></div><div><br></div><div>Then the R crash with error message:</div><div><br></div><div>
<div>*** caught segfault ***</div><div>address (nil), cause 'memory not mapped'</div><div><br></div><div>Traceback:</div><div> 1: .External(class__newInstance, module, pointer, ...)</div><div> 2: new_CppObject_xp(fields$.module, fields$.pointer, ...)</div>
<div> 3: Rcpp:::cpp_object_initializer(.self, .refClassDef, ...)</div><div> 4: .Object$initialize(...)</div><div> 5: initialize(value, ...)</div><div> 6: initialize(value, ...)</div><div> 7: new(PortPL, datel, list(hjs = colse), initM)</div>
<div><br></div><div>Possible actions:</div><div>1: abort (with core dump, if enabled)</div><div>2: normal R exit</div><div>3: exit R without saving workspace</div><div>4: exit R saving workspace</div><div>Selection:</div>
</div><div><br></div><div>######################################################################</div><div>I just can not figure out what is wrong, Is there anyone can help me check out? Thanks in advance!</div><div><br></div>
<div><br></div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div>