[R] allocating memory in a C module
Rajarshi Guha
rxg218 at psu.edu
Fri Oct 3 23:03:55 CEST 2003
Hi,
I'm using a package that has a number of formats. I have C code to
parse these formats the results of which are generally integer arrays.
I would like to utilize these modules in R rather than writing R code to
read in these files (and also to learn about R extensions).
Essentially what I want is to do something like this:
load_tsets <- function(t,c,p)
.C( "c_load_tsets",
as.integer(t),
as.integer(c),
as.integer(p))
t,c,p should be R list objects, and empty when supplied to the function
load_tsets. On return each one will contain a list of integers. Thus in
the C function I need to allocate storage for the 3 arrays.
I've tried using Realloc but when I run the function the returned list
does not show an increase in length. I've included a simplified version
of the C code below:
#include<R.h>
void t(int *v)
{
int i;
v = (int*) Realloc(v,5,int*);
for (i = 0; i < 5; i++)
v[i] = i*i;
printf("Hello\n");
return;
}
I load it into R by doing:
> dyn.load('t.so')
> ft <- function(x)
+ .C('t',
+ as.integer(x))
>
Then I do
> x <- c()
> ret <- ft(x)
Hello
> ret
[[1]]
numeric(0)
Do I need to go to the .Call interface?
Is there any code that comes with the R distro (or if anybody has some
similar code that I could look at)?
Any pointers would be appreciated,
thanks,
-------------------------------------------------------------------
Rajarshi Guha <rxg218 at psu.edu> <http://jijo.cjb.net>
GPG Fingerprint: 0CCA 8EE2 2EEB 25E2 AB04 06F7 1BB9 E634 9B87 56EE
-------------------------------------------------------------------
All life evolves by the differential survival of replicating entities.
-- Dawkins
More information about the R-help
mailing list