Skip to content
Prev 3993 / 10988 Next

[Rcpp-devel] function created from cxxfunction modifies input?

Hi Joshua,

When you declare "NumericVector xx(x)" xx is a copy of the pointer x. But
they both address the same location in the memory.
What you need here is to do a "deep copy" of the vector x. The best was to
do that in Rcpp is to use the function "clone".
Also declaring your vector xx with "const" will guarantee that your vector
x will not be modified by the function.

So your code should look like this :
## Rcpp function body
src <- '
  const NumericVector xx(x);
  int n = xx.size();
  NumericVector res = clone(xx);
  int toggle = 0;
  int tot = 100;
  int max = 100;
 [...]
On Sun, Jul 8, 2012 at 1:20 AM, Joshua Wiley <jwiley.psych at gmail.com> wrote:

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