Thanks Douglas,<br><br>However just to be sure I get it, const is in fact protecting the pointer on origin, but not its values. Is there a way to protect the values ? This is running fine :<br>const Rcpp::NumericMatrix m_source(origin);<br>
m_source(0,0) = 0;<br><br>Etienne<br><br><br><div class="gmail_quote">2011/7/14 Douglas Bates <span dir="ltr"><<a href="mailto:bates@stat.wisc.edu">bates@stat.wisc.edu</a>></span><br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
The way to guarantee a deep copy is to use Rcpp::clone(). In general<br>
I recommend using a const qualifier on any vectors or arrays<br>
constructed directly from the function arguments. If they need to be<br>
coerced to another storage type they will end up being a deep copy of<br>
the original but to preserve the functional programming semantics you<br>
should regard any arguments passed to C++ function from R as<br>
read-only.<br>
<br>
I would rewrite your code fragment as<br>
<br>
const Rcpp::NumericMatrix m_source(origin);<br>
Rcpp::NumericMatrix m_copy = Rcpp::clone(m_source);<br>
<div><div></div><div class="h5"><br>
On Thu, Jul 14, 2011 at 11:36 AM, Etienne B. Racine <<a href="mailto:etiennebr@gmail.com">etiennebr@gmail.com</a>> wrote:<br>
> Hi list,<br>
><br>
> I'm struggling with the copy of my parameters. If I understand correctly, if<br>
> you ship an int matrix to the function, you'll get a deep copy. However if<br>
> it's a numeric matrix, then there's no copy and you get a matrix pointing on<br>
> your R matrix. That's funny when you try to debug as one might work and the<br>
> other not.<br>
><br>
> Here's an illustration<br>
> library(Rcpp); library(inline)<br>
><br>
> f1 <- cxxfunction(signature(origin="numeric"), body = "<br>
> Rcpp::NumericMatrix m_source(origin);<br>
> m_source(0,0) = 0;<br>
> cout << origin << endl << m_source << endl ; ",<br>
> include = "using namespace std;",<br>
> plugin="Rcpp")<br>
><br>
> m <- matrix(1:4,2,2)<br>
> x <- f1(m)<br>
><br>
> #0x9c7fe18<br>
> #0xb416a1a8 # different<br>
> m<br>
> # [,1] [,2]<br>
> #[1,] 1 3<br>
> #[2,] 2 4<br>
> m <- matrix(1:4/2,2,2)<br>
> x <- f1(m)<br>
> #0xb416a090<br>
> #0xb416a090 # no different<br>
> m<br>
> # [,1] [,2]<br>
> #[1,] 0 1.5<br>
> #[2,] 1 2.0<br>
><br>
> Now, what I'd like is to have a (deep) copy of that matrix, whatever the<br>
> type. I've tried<br>
> Rcpp::NumericMatrix m_copy = m_source; // of course it did not work, but<br>
> never know with vectorisation ;-)<br>
> sdt::copy(m_source.begin(), m_source.end(), std::back_inserter(m_copy)) //<br>
> nope<br>
><br>
> The closer I got was with<br>
> f2 <- cxxfunction(signature(origin="numeric"), body = "<br>
> Rcpp::NumericMatrix m_source(origin);<br>
> Rcpp::NumericMatrix m_copy;<br>
> Rcpp::NumericMatrix::iterator itr;<br>
> cout << m_copy << endl << endl;<br>
> m_source(0,0) = 0;<br>
> for(itr = m_source.begin(); itr != m_source.end(); ++itr)<br>
> m_copy.push_back(*itr);<br>
> m_copy[3] = 1; // matrix structure is lost or never<br>
> existed;<br>
> cout << origin << endl << m_source << endl << m_copy ;<br>
> return wrap(m_copy);",<br>
> include = "using namespace std;",<br>
> plugin="Rcpp", verbose= FALSE)<br>
><br>
> Main drawback is that I loose the matrix structure. I'm sure there's a<br>
> correct way to do this, but I couldn't find it.<br>
><br>
> Etienne<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>
><br>
><br>
</blockquote></div><br>