Re: [R] Problem with C extension

From: Markus Schmidberger <schmidb_at_ibe.med.uni-muenchen.de>
Date: Mon 22 Jan 2007 - 17:00:29 GMT


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



R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Tue Jan 23 04:05:42 2007

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Mon 22 Jan 2007 - 17:30:33 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.