[Rcpp-devel] Rcpp matrix generation crashes my R session

Marc Jekel mjekel at uni-bonn.de
Sat Jun 22 18:10:17 CEST 2013

Dear Rcpp users,

I am a beginner, I was inspired by the new book by Eddelbuettel and just 
started to use Rccp. So far, everything works fine except for one 
function. The strange thing about it is that I runs perfectly when I 
call it several times but crashes my R session at some point (usually 
after the third call). I checked it on two Win 8 PCS to make sure it is 
nothing related to my PC.

Here is the code:


#include <Rcpp.h>
using namespace Rcpp;

// [[Rcpp::export]]
NumericMatrix transPCSpairwiseComp_C(
NumericVector validities,
NumericMatrix pattern,
int numbCues

NumericMatrix weightsNet(numbCues+3,numbCues+3);

for(int loop = 0; loop < numbCues; loop++){

   weightsNet(0,1+loop) = validities[loop];

for(int loopCol = 0; loopCol < numbCues; loopCol++){
for(int loop = 0; loop < numbCues; loop++){
weightsNet(1+loopCol, numbCues + 1 + loop)=


weightsNet(numbCues+1, numbCues+1) = 0;
weightsNet(numbCues+1, numbCues+2) = -.2;

for(int LoopCol = 0; LoopCol < weightsNet.nrow(); LoopCol++){
for(int LoopRow = 0; LoopRow < weightsNet.nrow(); LoopRow++){
weightsNet(LoopCol,LoopRow) = weightsNet(LoopCol,LoopRow) + 

return weightsNet;


So it is very basic code, a matrix is created based on three inputs (the 
vector "validities", the matrix "pattern", and the integer "numbCues"). 
My impression is that the very first command (creating the matrix 
weightsNet) is problematic because R also crashes when I delete all 
other code (although usually not after the third time I call it).

Thanks for any hint!


More information about the Rcpp-devel mailing list