Hi Joshua,<div><br></div><div>When you declare "NumericVector xx(x)" xx is a copy of the pointer x. But they both address the same location in the memory.</div><div>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".</div>
<div>Also declaring your vector xx with "const" will guarantee that your vector x will not be modified by the function.</div><div><br></div><div>So your code should look like this :</div><div><span style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:12.800000190734863px;background-color:rgb(255,255,255)">## Rcpp function body</span><br style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:12.800000190734863px;background-color:rgb(255,255,255)">
<span style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:12.800000190734863px;background-color:rgb(255,255,255)">src <- '</span><br style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:12.800000190734863px;background-color:rgb(255,255,255)">
<span style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:12.800000190734863px;background-color:rgb(255,255,255)"> const NumericVector xx(x);</span><br style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:12.800000190734863px;background-color:rgb(255,255,255)">
<span style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:12.800000190734863px;background-color:rgb(255,255,255)"> int n = xx.size();</span><br style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:12.800000190734863px;background-color:rgb(255,255,255)">
<span style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:12.800000190734863px;background-color:rgb(255,255,255)"> NumericVector res = clone(xx);</span><br style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:12.800000190734863px;background-color:rgb(255,255,255)">
<span style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:12.800000190734863px;background-color:rgb(255,255,255)"> int toggle = 0;</span><br style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:12.800000190734863px;background-color:rgb(255,255,255)">
<span style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:12.800000190734863px;background-color:rgb(255,255,255)"> int tot = 100;</span><br style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:12.800000190734863px;background-color:rgb(255,255,255)">
<span style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:12.800000190734863px;background-color:rgb(255,255,255)"> int max = 100;</span></div><div><span style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:12.800000190734863px;background-color:rgb(255,255,255)"> [...]</span></div>
<div><br><div class="gmail_quote">On Sun, Jul 8, 2012 at 1:20 AM, Joshua Wiley <span dir="ltr"><<a href="mailto:jwiley.psych@gmail.com" target="_blank">jwiley.psych@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Hi,<br>
<br>
I am familiar with R but quite new to C++. I am reading a C++<br>
textbook, so if this is an obvious question just resulting in my<br>
ignorance of C++, please let me know.<br>
<br>
I tried to translate someone else's R loop that was slow into c++ to<br>
use with Rcpp and the inline package:<br>
<br>
require(Rcpp)<br>
require(inline)<br>
<br>
## Rcpp function body<br>
src <- '<br>
NumericVector xx(x);<br>
int n = xx.size();<br>
NumericVector res(xx);<br>
int toggle = 0;<br>
int tot = 100;<br>
int max = 100;<br>
<br>
typedef NumericVector::iterator vec_iterator;<br>
vec_iterator ixx = xx.begin();<br>
vec_iterator ires = res.begin();<br>
for (int i = 0; i < n; i++) {<br>
if (ixx[i] != 0) {<br>
if (toggle == 1) {<br>
ires[i] = 0;<br>
}<br>
if (toggle != 1) {<br>
tot += ixx[i];<br>
if (tot > max) {<br>
max = tot;<br>
}<br>
if (tot <= max) {<br>
if (.98 * max > tot) {<br>
toggle = 1;<br>
}<br>
}<br>
}<br>
}<br>
if (ixx[i] == 0) {<br>
tot = 100;<br>
max = 100;<br>
toggle = 0;<br>
}<br>
}<br>
return res;<br>
'<br>
<br>
## compile the function<br>
foo.rcpp <- cxxfunction(signature(x = "numeric"), src, plugin = "Rcpp")<br>
<br>
## here is a little input vector, the function should<br>
## output the same but with a 0 instead of 8<br>
> d<br>
[1] 0 0 0 1 3 4 5 -1 2 3 -5 8<br>
> foo.rcpp(d) # great it works!<br>
[1] 0 0 0 1 3 4 5 -1 2 3 -5 0<br>
> d # but now d itself has changed?<br>
[1] 0 0 0 1 3 4 5 -1 2 3 -5 0<br>
<br>
It seems particularly surprising to me, because I do not think I even<br>
modify the input vector, I copy it into the numeric vector res and<br>
alter that.<br>
<br>
Thanks,<br>
<br>
Josh<br>
<span class="HOEnZb"><font color="#888888"><br>
<br>
--<br>
Joshua Wiley<br>
Ph.D. Student, Health Psychology<br>
Programmer Analyst II, Statistical Consulting Group<br>
University of California, Los Angeles<br>
<a href="https://joshuawiley.com/" target="_blank">https://joshuawiley.com/</a><br>
_______________________________________________<br>
Rcpp-devel mailing list<br>
<a href="mailto:Rcpp-devel@lists.r-forge.r-project.org">Rcpp-devel@lists.r-forge.r-project.org</a><br>
<a href="https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel" target="_blank">https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel</a><br>
</font></span></blockquote></div><br></div>