From dankoc at gmail.com  Wed Mar  5 16:56:35 2008
From: dankoc at gmail.com (Charles Danko)
Date: Wed, 5 Mar 2008 10:56:35 -0500
Subject: [Rd] SEXP size management.
Message-ID: <8adccabf0803050756j50ec2022nf9a028f39d8aedfd@mail.gmail.com>

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



[Rd] SEXP size management.
Charles Danko