<!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 bgcolor="#ffffff" text="#000000">
    Dear list members,<br>
    <br>
    I have started developing a package using the latest version of Rcpp
    just recently, so I am more or less an Rcpp newbie. Since I am not
    sufficiently proficient in advanced C++ (and admittedly too lazy
    too) to analyze the Rcpp source code, I have some questions
    regarding what actually happens inside Rcpp. Let me bother you with
    an example:<br>
    <br>
    RcppExport SEXP test(SEXP mat)<br>
    {<br>
    BEGIN_RCPP<br>
    &nbsp;&nbsp;&nbsp; Rcpp::NumericMatrix matC(mat);<br>
    &nbsp;&nbsp;&nbsp; Rcpp::NumericMatrix matD(mat);<br>
    <br>
    &nbsp;&nbsp;&nbsp; matC(1, 1) = 1;<br>
    &nbsp;&nbsp;&nbsp; matD(1, 1) = -matD(1, 1);<br>
    <br>
    &nbsp;&nbsp;&nbsp; return(matC);<br>
    END_RCPP<br>
    }<br>
    <br>
    If I call this function on some matrix, then the function returns
    the input matrix with its element [2, 2] set to 1, but that's all.
    The change in matD only seems to have an effect on the local copy
    matD. If I replace "return(matC)" by "return(matD)", then the
    function returns the input matrix with the sign of its element [2,
    2] inverted, so the change in matC has no effect. That Rcpp creates
    local copies, i.e. that matC and matD are independent objects each
    holding local copies of the argument mat, would perfectly explain
    these results. Previously, however, I thought Rcpp would only wrap
    an R/SEXP object into an Rcpp object without making a copy. If it
    was so, no matter whether the function returns matC or matD, the
    function would return the input matrix with -1 on position [2, 2].
    Did I get this point wrong? I suppose it would save time and memory
    not to create a copy, in particular, since arguments to functions
    are local copies already. Is there a way to avoid copying? <br>
    <br>
    A related question is what the "=" operator does. Does<br>
    <br>
    &nbsp;&nbsp;&nbsp; Rcpp::NumericVector x = ...;<br>
    <br>
    create a copy or is it only a pointer that is assigned?<br>
    <br>
    My final question concerns the "longevity" of Rcpp objects. Suppose
    I do the following:<br>
    <br>
    &nbsp;&nbsp;&nbsp; {<br>
    &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; Rcpp::NumericVector vec(10);<br>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; double *p = vec.begin();<br>
    &nbsp;&nbsp;&nbsp; }<br>
    <br>
    Can I use *p outside this block or is the pointer killed along with
    the variable vec after the block is closed? According to my limited
    knowledge, I would assume that it is only the reference vec that is
    not available outside the block, but that the corresponding R object
    still exists and will be deleted by the R garbage collector only
    after the whole function has terminated.<br>
    <br>
    Any help is gratefully appreciated! I am sorry if these are stupid
    questions or if any of them have been discussed previously.<br>
    <br>
    Thanks and best regards,<br>
    Ulrich<br>
    <tt><font size="2"><br>
      </font></tt>
  </body>
</html>