[Rcpp-devel] Segfault error during simulation in Rcpp

Xiao He praguewatermelon at gmail.com
Thu May 16 20:22:51 CEST 2013


Hi Dirk,

Does the code crash your R every time you run it or only occasionally? I
ran your sample code exactly the way it's written, and my R did not crash.

> myFun <- cppFunction('NumericMatrix myFun(NumericMatrix input, int n){
+   NumericMatrix A(n, n);
+   for(int Row = 0; Row < n; Row++) {
+     for(int Col = 0; Col < n; Col++) {
+       A(Row, Col) = input(Row, Col);
+     }
+   }
+   return A;
+ }')
>
> n <- 10
> x <- 1:n^2
> N <- 1e6
> b <- 0
> for (j in 1:N) {
+     means <- matrix(x, n, n)
+     res <- myFun(means, n)
+     a <- res[1, 1]
+     b <- b + a
+ }
> cat(sprintf("Done, b is %d\n", b))
Done, b is 1000000
>




On Thu, May 16, 2013 at 11:02 AM, Dirk Eddelbuettel <edd at debian.org> wrote:

>
> Here is the self-contained example I asked for.
>
> And yes, it crashes for me too.  So let's not create 1e6 temp matrices.
> Until someone has time to debug memory management internals. Which is
> really
> hard, so this may not get fixed for a while.  Sorry.
>
>
> #!/usr/bin/Rscript
>
> library(Rcpp)
> myFun <- cppFunction('NumericMatrix myFun(NumericMatrix input, int n){
>   NumericMatrix A(n, n);
>   for(int Row = 0; Row < n; Row++) {
>     for(int Col = 0; Col < n; Col++) {
>       A(Row, Col) = input(Row, Col);
>     }
>   }
>   return A;
> }')
>
> n <- 10
> x <- 1:n^2
> N <- 1e6
> b <- 0
> for (j in 1:N) {
>     means <- matrix(x, n, n)
>     res <- myFun(means, n)
>     a <- res[1, 1]
>     b <- b + a
> }
>
> cat(sprintf("Done, b is %d\n", b))
>
>
> --
> Dirk Eddelbuettel | edd at debian.org | http://dirk.eddelbuettel.com
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20130516/ec03b83e/attachment-0001.html>


More information about the Rcpp-devel mailing list