<div dir="ltr"><div style="font-family:arial,sans-serif;font-size:13px">Hi Dirk,</div><div style="font-family:arial,sans-serif;font-size:13px"><br></div><div style="font-family:arial,sans-serif;font-size:13px">Thanks for your quick answer. I don't think Rcpp::clone is what I was looking for. I know `stl_sort_inplace(a)` modify the value of `a`, but it surprise me to see it modify `b`. And it may modify some other variables c, d, e, f..., and it's hard to know which variables point to the same place.</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Oct 21, 2014 at 8:31 PM, Dirk Eddelbuettel <span dir="ltr"><<a href="mailto:edd@debian.org" target="_blank">edd@debian.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class=""><br>
On 21 October 2014 at 20:22, Chenliang Xu wrote:<br>
| Hello,<br>
|<br>
| With the following inplace sorting example, I understand the value of `a` is<br>
| sorted inplace, but it's strange to see the value of `b` is also modified. This<br>
| can cause some hard to detect bug, since the cpp function may modify a variable<br>
| defined in other scope.<br>
<br>
</span>Very well known issue -- maybe do a search for 'Rcpp::clone' ...<br>
<br>
In a nutshell, SEXP objects are passed by a __pointer__ and changes do<br>
therefore persist.  If you want distinct copies, use Rcpp::clone().<br>
<br>
Dirk<br>
<div><div class="h5"><br>
| It seems that rcpp doesn't respect the named field, which is adopted by R to<br>
| implement copy-on-modify. I don's see an easy fix on C++ side, since the called<br>
| cpp function has no information about variable binding in R. A possible fix is<br>
| adding a function `inplace` to R, which ensure the returned variable has named<br>
| filed = 0 so is safe to modify inplace. Then, we have to call the function as<br>
| `stl_sort_inplace(inplace(a))`, which seems odd but is also informative. It<br>
| shows clearly that we are breaking the pass-by-value rule in R.<br>
|<br>
| ```cpp<br>
| #include <Rcpp.h><br>
| using namespace Rcpp;<br>
|<br>
| // [[Rcpp::export]]<br>
| void stl_sort_inplace(NumericVector x) {<br>
|     std::sort(x.begin(), x.end());<br>
| }<br>
|<br>
| ```<br>
|<br>
| ```r<br>
| a <- seq(1, 0.1, -0.1)<br>
| b <- a<br>
| #  [1] 1.0 0.9 0.8 0.7 0.6 0.5 0.4 0.3 0.2 0.1<br>
|<br>
| stl_sort_inplace(a)<br>
|<br>
| a<br>
| #  [1] 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0<br>
|<br>
| b<br>
| #  [1] 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0<br>
|<br>
| a <- seq(1, 0.1, -0.1)<br>
| pure_function <- function (x) {<br>
|   y <- x<br>
|   stl_sort_inplace(y)<br>
|   print(y)<br>
| }<br>
| pure_function(a)<br>
| a<br>
| #  [1] 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0<br>
|<br>
| ```<br>
|<br>
</div></div>| _______________________________________________<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>
<span class="HOEnZb"><font color="#888888"><br>
--<br>
<a href="http://dirk.eddelbuettel.com" target="_blank">http://dirk.eddelbuettel.com</a> | @eddelbuettel | <a href="mailto:edd@debian.org">edd@debian.org</a><br>
</font></span></blockquote></div><br></div>