[R] Problem with C extension

Markus Schmidberger schmidb at ibe.med.uni-muenchen.de
Mon Jan 22 18:00:29 CET 2007


Hello,
thanks for help and code.
We did a lot of work to speedup our function in R. We have a nested 
loop, vectorizing is the fastest way. But there we got a very big matrix 
and problems with memory. So we want to stay by loops an speedup with C.

My code is similar to this. (my_c is code from Brian D. Ripley)

SEXP test(SEXP a, SEXP b, SEXP in)
{
    SEXP ans, new;
    int n=INTEGER(in)[0],i,j;
    PROTECT(ans = allocVector(REALSXP, 1));
    REAL(ans)[0]=REAL(a)[0];
/*    for(j = 0; i < m; j++)*/
    for(i = 0; i < n; i++)
    {
       /* b= ... ^i ....*j*/
        PROTECT(new = allocVector(REALSXP, i+2));
        new = my_c(ans,b);
        PROTECT(ans = allocVector(REALSXP, i+2));
        ans = new;
        UNPROTECT(2);
    }
    UNPROTECT(1);
    return ans;
}

We get an error by in=1300

 > .Call("test",1,3,as.integer(1300));
Fehler: type mismatch
 > .Call("test",1,3,as.integer(1300));
Speicherzugriffsfehler

Is there a possibility to free allocated memory? free(...) does not work.
Is there a possibility to reallocate a vector?

Thanks a lot
Markus



More information about the R-help mailing list