[Rcpp-devel] function modifying it self its argument

Pierre GLOAGUEN Pierre.Gloaguen at ifremer.fr
Thu Feb 26 16:16:49 CET 2015


Hello everybody,

I have a very simple example
I have a vector vec_CMA which length is a multiple of 6.
I want to get the exact same vector, except the last element which is 
the exponential of the last element of vec_CMA
The code is the following

//myfun.cpp
#include <Rcpp.h>
using namespace Rcpp;

// [[Rcpp::export]]
NumericVector par_CMAtR(NumericVector vec_CMA){
   int K = (vec_CMA.size())/6;
   NumericVector out(6*K);
   out = vec_CMA;
   out[6*K-1] = exp(vec_CMA[6*K-1]);
   return out;
}

I apply the function with the R code
sourceCpp("myfun.cpp")
vec_C <- rep(1,6)
par_CMAtR(vec_C)
[1] 1 1 1 1 1 2.718282

8

works fine. Except the vec_C is modified too!
vec_C
[1] 1 1 1 1 1 2.718282

It's the first time I have this kind of problem. What is wrong in my code?
Thanks for your help,

Pierre Gloaguen

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20150226/97703046/attachment.html>


More information about the Rcpp-devel mailing list