[Rd] SEXP size management.
Charles Danko
dankoc at gmail.com
Wed Mar 5 17:33:36 CET 2008
Hi, Oleg,
Thanks very much for your answer! I was indeed confused; thinking
that Realloc was intended to be an analog to realloc (except for SEXP
variables).
Is there a way to do either of the following:
-- resize the SEXP directly (for my purpose, I ONLY need to decrease
the size), or
-- assign it to to a C pointer that was allocated and used previously
in the function (and can thus be resized with realloc).
Thanks again,
Charles
On Wed, Mar 5, 2008 at 11:14 AM, Oleg Sklyar <osklyar at ebi.ac.uk> wrote:
> Rblah in your example is a SEXP structure. Realloc is an interface to C
> realloc and is not intended to resize SEXP structures -- it is used to
> resize user-controlled memory (which is generally not created by
> allocVector/INTEGER).
>
> You would expect a call like:
>
> int * val;
> ...
> Realloc(val, 20, int);
>
> Best,
> Oleg
>
>
>
> Charles Danko wrote:
> > Hi,
> >
> > Trying to decrease the size of a SEXP variable without reassigning
> > values individually in a loop.
> >
> > So far, I've tried using Realloc, as the follow source demonstrates:
> > SEXP dothis() {
> > SEXP Rblah;
> > PROTECT(Rblah = NEW_INTEGER(6));
> >
> > int* blah = INTEGER(Rblah);
> > blah[0] = 1;
> > blah[1] = 2;
> > blah[2] = 3;
> >
> > Realloc(Rblah, 3, INTEGER);
> >
> > UNPROTECT(1);
> > return(Rblah);
> > }
> >
> > According to the documentation, I think that this should work, however
> > it returns an error on compile: "test.c:17: error: expected expression
> > before ')' token" (line 17 refers to the Realloc line).
> >
> > Another solution that will suit my needs is managing the variable in C
> > and assigning the pointer to an R type in the end. The following code
> > gets at this, but int* and SEXP, INTEGER are incompatible:
> > SEXP dothat() {
> > int* blah = malloc(3);// = INTEGER(Rblah);
> > blah[0] = 1;
> > blah[1] = 2;
> > blah[2] = 3;
> >
> > SEXP Rblah;
> > PROTECT(Rblah = blah);
> >
> > UNPROTECT(1);
> > return(Rblah);
> > }
> >
> > Any suggestions for someone still new to SEXP memory management?
> >
> > Thanks,
> > Charles
> >
> > ______________________________________________
> > R-devel at r-project.org mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-devel
>
> --
> Dr Oleg Sklyar * EBI-EMBL, Cambridge CB10 1SD, UK * +44-1223-494466
>
More information about the R-devel
mailing list