<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">Pretty much. But sometimes that's what you want, and Rcpp does not get in the way. You just have to know the rules of the game. </div><div class="">BTW, same rules apply when you use .Call C/R API, you are in charge of making the copy when it's needed. </div><div class=""><br class=""></div><div class="">Romain</div><br class=""><div><blockquote type="cite" class=""><div class="">Le 22 oct. 2014 à 17:53, Chenliang Xu <<a href="mailto:luckyrand@gmail.com" class="">luckyrand@gmail.com</a>> a écrit :</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class=""><span style="font-family:arial,sans-serif;font-size:13px" class="">Thanks a lot!</span><div style="font-family:arial,sans-serif;font-size:13px" class=""><br class=""></div><div style="font-family:arial,sans-serif;font-size:13px" class="">Does that mean we should never modify an argument passed from R to cpp?</div></div><div class="gmail_extra"><br class=""><div class="gmail_quote">On Wed, Oct 22, 2014 at 8:24 AM, Romain Francois <span dir="ltr" class=""><<a href="mailto:romain@r-enthusiasts.com" target="_blank" class="">romain@r-enthusiasts.com</a>></span> wrote:<br class=""><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word" class="">a and b are the same object: <div class=""><br class=""></div><div class=""><span class=""><div class="">> a <- seq(1, 0.1, -0.1)</div><div class="">> b <- a</div></span><div class="">> pryr::address( a )</div><div class="">[1] "0x7f9504534948"</div><div class="">> pryr::address( b )</div><div class="">[1] "0x7f9504534948"</div><div class=""><br class=""></div><div class="">So clone is what you need here. </div><div class=""><br class=""></div><div class="">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 class=""><br class=""></div><div class="">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 class=""><br class=""></div><div class="">Instead, the responsibility is given to the user to clone explicitly when changes will be made to the underlying object. </div><div class=""><br class=""></div><div class="">Romain </div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><div class=""><blockquote type="cite" class=""><div class="">Le 22 oct. 2014 à 04:13, Chenliang Xu <<a href="mailto:luckyrand@gmail.com" target="_blank" class="">luckyrand@gmail.com</a>> a écrit :</div><div class=""><div class="h5"><br class=""><div class=""><div dir="ltr" class=""><div style="font-family:arial,sans-serif;font-size:13px" class="">Hi Dirk,</div><div style="font-family:arial,sans-serif;font-size:13px" class=""><br class=""></div><div style="font-family:arial,sans-serif;font-size:13px" class="">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 class=""><div class="gmail_quote">On Tue, Oct 21, 2014 at 8:31 PM, Dirk Eddelbuettel <span dir="ltr" class=""><<a href="mailto:edd@debian.org" target="_blank" class="">edd@debian.org</a>></span> wrote:<br class=""><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class=""><br class="">
On 21 October 2014 at 20:22, Chenliang Xu wrote:<br class="">
| Hello,<br class="">
|<br class="">
| With the following inplace sorting example, I understand the value of `a` is<br class="">
| sorted inplace, but it's strange to see the value of `b` is also modified. This<br class="">
| can cause some hard to detect bug, since the cpp function may modify a variable<br class="">
| defined in other scope.<br class="">
<br class="">
</span>Very well known issue -- maybe do a search for 'Rcpp::clone' ...<br class="">
<br class="">
In a nutshell, SEXP objects are passed by a __pointer__ and changes do<br class="">
therefore persist.  If you want distinct copies, use Rcpp::clone().<br class="">
<br class="">
Dirk<br class="">
<div class=""><div class=""><br class="">
| It seems that rcpp doesn't respect the named field, which is adopted by R to<br class="">
| implement copy-on-modify. I don's see an easy fix on C++ side, since the called<br class="">
| cpp function has no information about variable binding in R. A possible fix is<br class="">
| adding a function `inplace` to R, which ensure the returned variable has named<br class="">
| filed = 0 so is safe to modify inplace. Then, we have to call the function as<br class="">
| `stl_sort_inplace(inplace(a))`, which seems odd but is also informative. It<br class="">
| shows clearly that we are breaking the pass-by-value rule in R.<br class="">
|<br class="">
| ```cpp<br class="">
| #include <Rcpp.h><br class="">
| using namespace Rcpp;<br class="">
|<br class="">
| // [[Rcpp::export]]<br class="">
| void stl_sort_inplace(NumericVector x) {<br class="">
|     std::sort(x.begin(), x.end());<br class="">
| }<br class="">
|<br class="">
| ```<br class="">
|<br class="">
| ```r<br class="">
| a <- seq(1, 0.1, -0.1)<br class="">
| b <- a<br class="">
| #  [1] 1.0 0.9 0.8 0.7 0.6 0.5 0.4 0.3 0.2 0.1<br class="">
|<br class="">
| stl_sort_inplace(a)<br class="">
|<br class="">
| a<br class="">
| #  [1] 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0<br class="">
|<br class="">
| b<br class="">
| #  [1] 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0<br class="">
|<br class="">
| a <- seq(1, 0.1, -0.1)<br class="">
| pure_function <- function (x) {<br class="">
|   y <- x<br class="">
|   stl_sort_inplace(y)<br class="">
|   print(y)<br class="">
| }<br class="">
| pure_function(a)<br class="">
| a<br class="">
| #  [1] 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0<br class="">
|<br class="">
| ```<br class="">
|<br class="">
</div></div>| _______________________________________________<br class="">
| Rcpp-devel mailing list<br class="">
| <a href="mailto:Rcpp-devel@lists.r-forge.r-project.org" target="_blank" class="">Rcpp-devel@lists.r-forge.r-project.org</a><br class="">
| <a href="https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel" target="_blank" class="">https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel</a><br class="">
<span class=""><font color="#888888" class=""><br class="">
--<br class="">
<a href="http://dirk.eddelbuettel.com/" target="_blank" class="">http://dirk.eddelbuettel.com</a> | @eddelbuettel | <a href="mailto:edd@debian.org" target="_blank" class="">edd@debian.org</a><br class="">
</font></span></blockquote></div><br class=""></div>
_______________________________________________<br class="">Rcpp-devel mailing list<br class=""><a href="mailto:Rcpp-devel@lists.r-forge.r-project.org" target="_blank" class="">Rcpp-devel@lists.r-forge.r-project.org</a><br class=""><a href="https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel" target="_blank" class="">https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel</a></div></div></div></blockquote></div><br class=""></div></div></div></blockquote></div><br class=""></div>
_______________________________________________<br class="">Rcpp-devel mailing list<br class=""><a href="mailto:Rcpp-devel@lists.r-forge.r-project.org" class="">Rcpp-devel@lists.r-forge.r-project.org</a><br class="">https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel</div></blockquote></div><br class=""></body></html>