<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>

    <meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
  </head>
  <body text="#000000" bgcolor="#ffffff">
    Dear Rcpp Experts, <br>
    <br>
    I have encountered two issues when modifying an object within C++
    function. See the code below:<br>
    <br>
    ##########################################<br>
    library(inline); library(Rcpp)<br>
    D &lt;- matrix(rnorm(4),2,2)<br>
    src3 &lt;- '<br>
    &nbsp; NumericMatrix xD(D);<br>
    &nbsp; xD[1,1] = 100.0; <br>
    return xD;<br>
    '<br>
    passRef &lt;-
    cxxfunction(signature(D="matrix"),body=src3,plugin="Rcpp")<br>
    passRef(D); D<br>
    ###########################################<br>
    <br>
    From which I get the following:<br>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [,1]&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [,2]<br>
    [1,]&nbsp; -0.587375 0.3173949<br>
    [2,] 100.000000 0.1801181<br>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [,1]&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [,2]<br>
    [1,]&nbsp; -0.587375 0.3173949<br>
    [2,] 100.000000 0.1801181<br>
    <br>
    I have two questions here:<br>
    1. Why is the [2,1] modified, when I would have expected the [2,2]
    entry to be modified, given that I had assign 100.0 to xD[1,1] in
    C++?<br>
    2. Why does the change to the object D <b>persist</b> when we call
    the object again? This looks like D has been passed by reference and
    is therefore affected by what happened within the function. However,
    this is not the expected behaviour for an R function. What's going
    on here?<br>
    <br>
    Thank you very much for your help,<br>
    Cedric <br>
    <br>
    <br>
    <div class="moz-signature">-- <br>
      <div class="moz-signature"><font face="Times" size="3">
          Cedric Ginestet <br>
          Centre for Neuroimaging Sciences (L3.04) <br>
          NIHR Biomedical Research Centre <br>
          Department of Neuroimaging <br>
          Institute of Psychiatry, Box P089 <br>
          King's College London <br>
          De Crespigny Park<br>
          London <br>
          SE5 8AF <br>
          Tel: (+44) 20-3228-3052<br>
          Fax: (+44) 20-3228-2116<br>
          Email: <a class="moz-txt-link-abbreviated"
            href="mailto:cedric.ginestet@kcl.ac.uk">cedric.ginestet@kcl.ac.uk</a><br>
        </font>
      </div>
    </div>
  </body>
</html>