<p dir="ltr">Hello David,<br>
The general answer to your question is, if the type of your matrix (integer or numeric) in R is different from the one you declare in Rcpp, Rcpp will make a copy and cast it to the appropriate type.<br>
For example, 1:12 is of type integer, and if you pass it as a NumericMatrix, Rcpp will implicitly copy the whole matrix, so no matter what modification you did, the original matrix will not change.<br></p>
<p dir="ltr">Best,<br>
Yixuan</p>
<div class="gmail_quote">On Nov 26, 2014 1:33 AM, "David Shih" <<a href="mailto:david.shih@mail.utoronto.ca">david.shih@mail.utoronto.ca</a>> wrote:<br type="attribution"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hello,<br>
<br>
There was an earlier post on this subject, but based on my experimentation, the behaviour of modifying input argument is different depending on how the matrix was initialization and other factors...<br>
<br>
I wrote a Rcpp function to modify an input matrix. After calling this function, the input matrix is modified under some circumstances and not modified under others. The behaviour is the same on repeat runs and on both Linux (3.16.3-1-ARCH) and Mac (OSX 10.9).<br>
<br>
The R script, C++ code, and the results are available on Bitbucket:<br>
<br>
<a href="https://bitbucket.org/dshih/rcpp_inplace" target="_blank">https://bitbucket.org/dshih/rcpp_inplace</a><br>
<br>
I don't quite understand when the input matrix is modified in place by the Rcpp function and when the input matrix is be copied on write in the Rcpp function.<br>
<br>
When I stay within Rcpp/C++, a input argument can be modified in-place by a function. (This feature was critical to my optimization.)<br>
<br>
After the code returns to R, I would expect either copy-on-write or in-place modification but not both.<br>
<br>
What is the correct behaviour?<br>
<br>
<br>
Thank you,<br>
<br>
David J. H. Shih<br>
<br>
The Hospital for Sick Children<br>
Peter Gilgan Centre for Research and Learning<br>
686 Bay St<br>
17th floor, Room 17.9707<br>
Toronto, ON  M5G 0A4<br>
Canada<br>
Tel:  <a href="tel:%28416%29%20813-7654%20x309157" value="+14168137654">(416) 813-7654 x309157</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>
</blockquote></div>