[Rcpp-devel] Passing arguments by value: unexpected behaviour

Johannes Egner johannes.egner at gmail.com
Tue Sep 14 10:55:26 CEST 2010


Hello all,

Still taking my first steps, I've come across a feature (?) I cannot yet
make sense of. Consider a simple function with its argument passed by value:

SEXP fun(SEXP arg)
{
using namespace Rcpp;

NumericVector vec(arg);
for(int i=0; i<vec.size(); i++)
vec[i] += 1;

return vec;
}

The call in R is declared as

R.fun <- function(arg)
{
.Call( "fun", as.double(arg), PACKAGE = "wonderland" )
}


Using Rcpp 0.8.6 and R-2.11.1, I get

> arg <- 1:2 # or any vector of size > 1
> result <- R.fun(arg)
> result; arg
[1] 2 3
[1] 1 2

That's fine -- but if I take as argument a vector of size 1, I get

> arg <- 1
> result <- R.fun(arg)
> result; arg
[1] 2
[1] 2

What am I missing?
Thanks, Jo
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20100914/4f924b73/attachment.htm>


More information about the Rcpp-devel mailing list