<br><br><div class="gmail_quote">On Sat, Jan 15, 2011 at 4:15 AM, Romain Francois <span dir="ltr">&lt;<a href="mailto:romain@r-enthusiasts.com">romain@r-enthusiasts.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
Le 13/01/11 16:29, Dominick Samperi a écrit :<div class="im"><br>
<blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
The template expression code is very interesting, but it<br>
does not work as expected under<br>
Windows/g++/MinGW/32bit/Rterm.exe. The problem<br>
does not appear when I use Rgui.exe, or if I use<br>
64bit Windows!<br>
<br>
Consider the following C++ code called using<br>
.Call(&#39;testsugar&#39;,1:5,1:5):<br>
<br>
RcppExport SEXP testsugar(SEXP x_, SEXP y_) {<br>
     Rcpp::NumericVector x(x_), y(y_);<br>
     Rprintf(&quot;%d, %lf, %lf\n&quot;, (x+y).size(), (x+y)[0], (x+y)[1]);<br>
     return R_NilValue;<br>
}<br>
<br>
Under Linux/GCC, or 64bit Windows/g++, or<br>
32bit Windows/g++ I get the expected result:<br>
<br>
5, 2.0, 4.0<br>
<br>
Under Windows/32bit/Rterm.exe I get:<br>
<br>
5, 0.0, 0.0<br>
</blockquote>
<br></div>
Intriguing. Maybe Rprintf is to blame. Can you try this instead:</blockquote><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;"><div class="im">
<br>
<br>
RcppExport SEXP testsugar(SEXP x_, SEXP y_) {<br>
    Rcpp::NumericVector x(x_), y(y_);<br></div>
        int n = (x+y).size() ;<br>
        double xy0 = (x+y)[0] ;<br>
        double xy1 = (x+y)[1] ;<br>
    Rprintf(&quot;%d, %lf, %lf\n&quot;, n, xy0, xy1);<br>
    return R_NilValue;<div class="im"><br>
}<br></div></blockquote><div><br> Good guess, I found a problem with Rprintf yesterday, but this<br>
will not fix it. The problem is not caused by the arbitrary evaluation<br>
order of the arguments to Rprintf. It is caused by the different<br>
behavior of the format control &quot;%lf&quot; under different architectures!<br>
In particular, the problem goes away under i386/32bit Windows using<br>
MinGW/g++ when &quot;%lf&quot; is replaced with &quot;%f&quot;. I could not find this<br>
documented anywhere.<br><br>Of course, this has nothing to do with Rcpp, and it might<br>be of interest to r-devel.<br><br>For me this was just a distraction because the problem with<br>Visual C++ is still alive and well and has nothing to do with<br>
Rprintf. As this information might be helpful in other <br>(non-VC++) contexts here is what I am seeing.<br><br>Consider:<br><br>RcppExport SEXP testsugar(SEXP x_, SEXP y_) {<br>    Rcpp::NumericVector x(x_), y(y_);<br>    Rprintf(&quot;val = %f\n&quot;, (x+y)[0]);<br>
    return R_NilValue;<br>}<br><br>This goes through operator+(lhs,rhs) in plus.h, which triggers the<br>construction of an object of type Plus_Vector_Vector, and the<br>construction seems to happen without problems, because the<br>
following Rprint&#39;s print what you would expect in the<br>constructor:<br><br>Rprintf(&quot;RTYPE = %d\n&quot;, RTYPE); // get 14, REALSXP<br>Rprintf(&quot;size: %d, %d, %d\n&quot;, lhs.size(), rhs.size(), this-&gt;size()); // 5, 5, 5<br>
Rprintf(&quot;vals: %f, %f, %f, %f\n&quot;, lhs[0], rhs[0], lhs[1], rhs[1]); // 1.0, 1.0, 2.0, 2.0<br>Rprintf(&quot;ptrs: %p, %p\n&quot;, &amp;lhs, &amp;rhs); // reasonable addresses<br><br>But when you leave the constructor the two objects lhs and rhs get<br>
clobbered, even though their respective addresses seem not to<br>change. In particular, the following print statements when inserted<br>into operator[](int i) show garbage (and sometimes cause a crash):<br><br>Rprintf(&quot;ptrs: %p, %p\n&quot;, &amp;lhs, &amp;rhs); // this is OK, same addresses as above<br>
Rprintf(&quot;lhs_ = %f\n&quot;, lhs_); // 0.0 or garbage<br>Rprintf(&quot;rhs_ = %f\n&quot;, rhs_); // 0.0 or garbage<br>Rprintf(&quot;size = %d\n&quot;, lhs.size()); // usually crashes.<br><br>Dominick<br><br></div><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
<div class="im">
<br>
<blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
(Under VC++ there are more serious problems including<br>
corruption of other in the wrap-up function<br>
Vector(VectorBase&amp; other), but since VC++ is not<br>
supported I will not elaborate here.)<br>
<br>
Thanks,<br>
Dominick<br>
</blockquote>
<br>
<br></div>
-- <br>
Romain Francois<br>
Professional R Enthusiast<br>
+33(0) 6 28 91 30 30<br>
<a href="http://romainfrancois.blog.free.fr" target="_blank">http://romainfrancois.blog.free.fr</a><br>
|- <a href="http://bit.ly/fT2rZM" target="_blank">http://bit.ly/fT2rZM</a> : highlight 0.2-5<br>
|- <a href="http://bit.ly/gpCSpH" target="_blank">http://bit.ly/gpCSpH</a> : Evolution of Rcpp code size<br>
`- <a href="http://bit.ly/hovakS" target="_blank">http://bit.ly/hovakS</a> : RcppGSL initial release<br>
<br>
<br>
_______________________________________________<br>
Rcpp-devel mailing list<br>
<a href="mailto:Rcpp-devel@lists.r-forge.r-project.org" target="_blank">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><br>