From osklyar at ebi.ac.uk  Wed Mar  5 17:14:19 2008
From: osklyar at ebi.ac.uk (Oleg Sklyar)
Date: Wed, 05 Mar 2008 16:14:19 +0000
Subject: [Rd] SEXP size management.
In-Reply-To: <8adccabf0803050756j50ec2022nf9a028f39d8aedfd@mail.gmail.com>
References: <8adccabf0803050756j50ec2022nf9a028f39d8aedfd@mail.gmail.com>
Message-ID: <47CEC6DB.1030309@ebi.ac.uk>

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



[Rd] SEXP size management.
Oleg Sklyar