[R] Problem with C extension

Brian Ripley ripley at stats.ox.ac.uk
Mon Jan 22 18:06:12 CET 2007


On Mon, 22 Jan 2007, Markus Schmidberger wrote:

> 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)

And not in many ways similar to mine, hence your error message.
You really do have to handle all the types, as I did most.

>
> 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.

No, but garbage collection does.

> Is there a possibility to reallocate a vector?

Yes, sort of.  See lengthgets().

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595



More information about the R-help mailing list