<div dir="ltr"><span style="font-family:arial,sans-serif;font-size:13px">Thanks a lot!</span><div style="font-family:arial,sans-serif;font-size:13px"><br></div><div style="font-family:arial,sans-serif;font-size:13px">Does that mean we should never modify an argument passed from R to cpp?</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Oct 22, 2014 at 8:24 AM, Romain Francois <span dir="ltr"><<a href="mailto:romain@r-enthusiasts.com" target="_blank">romain@r-enthusiasts.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word">a and b are the same object: <div><br></div><div><span class=""><div>> a <- seq(1, 0.1, -0.1)</div><div>> b <- a</div></span><div>> pryr::address( a )</div><div>[1] "0x7f9504534948"</div><div>> pryr::address( b )</div><div>[1] "0x7f9504534948"</div><div><br></div><div>So clone is what you need here. </div><div><br></div><div>Implementing copy on write for that kind of example is possible, but would require a lot of additional code, i.e. the iterator would need to handle the write operation. </div><div><br></div><div>An undesirable side effect of this is that such iterators would be quite less performant, right now Rcpp is close to the metal and uses direct pointers as iterators when it makes sense. A price that everyone would have to pay. no go. </div><div><br></div><div>Instead, the responsibility is given to the user to clone explicitly when changes will be made to the underlying object. </div><div><br></div><div>Romain </div><div><br></div><div><br></div><div><div><blockquote type="cite"><div>Le 22 oct. 2014 à 04:13, Chenliang Xu <<a href="mailto:luckyrand@gmail.com" target="_blank">luckyrand@gmail.com</a>> a écrit :</div><div><div class="h5"><br><div><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><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><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" target="_blank">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><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" target="_blank">edd@debian.org</a><br>
</font></span></blockquote></div><br></div>
_______________________________________________<br>Rcpp-devel mailing list<br><a href="mailto:Rcpp-devel@lists.r-forge.r-project.org" target="_blank">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></div></div></div></blockquote></div><br></div></div></div></blockquote></div><br></div>