[Rd] SEXP size management.

Charles Danko dankoc at gmail.com
Wed Mar 5 16:56:35 CET 2008


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



More information about the R-devel mailing list