Slava, thanks for your pointers. I changed the code to the way Dirk example goes, namely<br>------------------------------------------------------<br>#include &lt;RcppArmadillo.h&gt;<br><br>
RcppExport void modify(SEXP mem){<br>
  Rcpp::NumericMatrix r_m(mem); <br>
  arma::mat m(r_m.begin(), r_m.nrow(), r_m.ncol(), false);<br>
  m.print();<br>
  m=m+m;<br>
}<br>-----------------------------------------------------------------<br>i compiled it, and run shared lib from R doing<br>&gt;m<br>
     [,1] [,2] [,3] [,4] [,5]<br>
[1,]    1    3    5    7    9<br>
[2,]    2    4    6    8   10<br><br>&gt;dyn.load(&quot;....so&quot;)<br>
&gt; .Call(&quot;modify&quot;, m)<br>&gt; m<br>
     [,1] [,2] [,3] [,4] [,5]<br>
[1,]    1    3    5    7    9<br>
[2,]    2    4    6    8   10<br><br><br>It didn&#39;t segfault, but the memory in R process didn&#39;t change as you see, although the matrix in armadillo code doubled. Which means that NumericMatrix copied memory from R process and passed a pointer to armadillo. I was hoping there is a way to have a shared memory between R and C++ on which one could operate, but i am beginning to understand that is not what is done in Rcpp, and probably this could not be done.<br>
<p><br></p><br><br><br><br> <br><br><div class="gmail_quote">On Tue, Nov 1, 2011 at 11:32 PM, Slava Razbash <span dir="ltr">&lt;<a href="mailto:slava.razbash@gmail.com">slava.razbash@gmail.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
Andre,<br>
<br>
You should compare your code with a working RcppArmadillo example,<br>
such as the one on Dirk&#39;s website:<br>
<a href="http://dirk.eddelbuettel.com/code/rcpp.armadillo.html" target="_blank">http://dirk.eddelbuettel.com/code/rcpp.armadillo.html</a><br>
&quot;Writing R Extensions&quot; descirbes the .Call() interface and the R API.<br>
When using .Call(), it does not make copies of the objects passed to<br>
it.<br>
<br>
Rcpp wraps around the R API so that you program with Rcpp objects.<br>
This simplifies memory management.<br>
In the code that you provided, you should instantiate Rcpp objects first.<br>
<br>
Further, if you  #include &lt;RcppArmadillo.h&gt;, then you don&#39;t have to<br>
#include &lt;Rcpp.h&gt; or #include &lt;armadillo&gt;.<br>
<br>
You could also read Software for data analysis: programming with R by<br>
John Chambers.<br>
<br>
<br>
Best Regards,<br>
<font color="#888888"><br>
Slava<br>
</font><div><div></div><div class="h5"><br>
On Wed, Nov 2, 2011 at 1:38 PM, andre zege &lt;<a href="mailto:andre.zege@gmail.com">andre.zege@gmail.com</a>&gt; wrote:<br>
&gt; Dirk, apologies, i meant to sent it to R-devel, i just replied to wrong<br>
&gt; list. Now that i read responses on both lists, i am confused. Simon Urbanek<br>
&gt; seemed to indicate that call by reference from<br>
&gt; R to C++ is impossible with .C interface and dangerous and unreliable with<br>
&gt; .Call. If i understood you correctly you seem to say that Rcpp facilitates<br>
&gt; call by reference from R to C++. I actually tried that<br>
&gt; in Rcpp as well but didn&#39;t succeed either, may be you could point me in the<br>
&gt; right direction. Here is what i tried<br>
&gt;<br>
&gt;<br>
&gt; modify.cpp<br>
&gt; ===========================<br>
&gt; #include &lt;Rcpp.h&gt;<br>
&gt; #include &lt;RcppArmadillo.h&gt;<br>
&gt; #include &lt;armadillo&gt;<br>
&gt; using namespace Rcpp;<br>
&gt; using namespace arma;<br>
&gt;<br>
&gt; RcppExport void modify(SEXP mem){<br>
&gt;   mat m=as&lt;mat&gt;(mem);<br>
&gt;   m.print();<br>
&gt;   m=m+m;<br>
&gt;<br>
&gt; }<br>
&gt; ==========================<br>
&gt; I compiled the above, pointing  to RccpArmadillo include dir and linked<br>
&gt; shared library to armadillo code. Then i loaded shared library and tried to<br>
&gt; run code from R as follows<br>
&gt;<br>
&gt;<br>
&gt;&gt;dyn.load(&quot;/home/az05625/testarma/passptr.so&quot;)<br>
&gt;&gt; .Call(&quot;modify&quot;, m)<br>
&gt;<br>
&gt; matrix m prints out fine, but code segfaults on the last statement m=m+m<br>
&gt; Could you give me some idea on how to fix this?<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt;&gt; Please ask basic R programming questions on R-devel as you seem to have<br>
&gt;&gt; read<br>
&gt;&gt; the wrong documentation --- there is no support for .C() in Rcpp. We do<br>
&gt;&gt; what<br>
&gt;&gt; we do via SEXP objects, and those require .Call().  So I suggest you read<br>
&gt;&gt; a<br>
&gt;&gt; little more in &quot;Writing R Extensions&quot;.  As well as the Rcpp documentation.<br>
&gt;&gt;<br>
&gt;&gt; And as you will learn in the &quot;Rcpp-introduction&quot; and other places, we use<br>
&gt;&gt; what is called proxy model --- so yes, we do pass pointers and no, you<br>
&gt;&gt; don;t<br>
&gt;&gt; get more lightweight than this.<br>
&gt;&gt;<br>
&gt;&gt; Then again, R uses copy-on-write so if you want to truly alter without<br>
&gt;&gt; having<br>
&gt;&gt; R create new copies for you then external pointers are your best bet.<br>
&gt;&gt;<br>
&gt;&gt; Dirk<br>
&gt;&gt;<br>
&gt;&gt; --<br>
&gt;&gt; &quot;Outside of a dog, a book is a man&#39;s best friend. Inside of a dog, it is<br>
&gt;&gt; too<br>
&gt;&gt; dark to read.&quot; -- Groucho Marx<br>
&gt;<br>
&gt;<br>
&gt;<br>
</div></div><div><div></div><div class="h5">&gt; _______________________________________________<br>
&gt; Rcpp-devel mailing list<br>
&gt; <a href="mailto:Rcpp-devel@lists.r-forge.r-project.org">Rcpp-devel@lists.r-forge.r-project.org</a><br>
&gt; <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>
&gt;<br>
</div></div></blockquote></div><br>