Re: [R] Problem with C extension

From: Brian Ripley <ripley_at_stats.ox.ac.uk>
Date: Mon 22 Jan 2007 - 17:06:12 GMT

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

______________________________________________
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:28:50 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 - 18:30:31 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.