[R] .C() memory allocation

Dan Davison davison at uchicago.edu
Fri Dec 5 20:27:58 CET 2003


I would like to retrieve a vector of integers from a call to .C(), but I
don't know its length in advance. How do I do this without making an ugly
safe guess?

My vector is called "sequences".
I am passing the argument sequences = integer(0) in the call to .C(),
then declaring the corresponding argument as int *sequences in my C code.

I tried R_alloc()ing the storage in C and assigning the pointer to
sequences but that segfaulted.

Then I tried Realloc()ing sequences:
sequences = Realloc(sequences, *nsam * *totalnmuts, int) ;

But that also segfaults.

Thanks very much for replies and R in general.

Dan



btw I think this is misleading / a typo in section 5.1.2 p.58 ch5 of
"Writing R Extensions", I hope I haven't just misunderstood.

It reads:
The interface functions are
	type* Calloc(size_t n, type)
 		     ^^^^^^
	type* Realloc(any *p, size_t n, type)
                              ^^^^^^

But RS.h makes me think that n should just be an integer giving the number
of elements and not necessarily be of type size_t:

extern void *R_chk_calloc(size_t, size_t);
extern void *R_chk_realloc(void *, size_t);

#define Calloc(n, t)   (t *) R_chk_calloc( (size_t) (n), sizeof(t) )
#define Realloc(p,n,t) (t *) R_chk_realloc( (void *)(p), (size_t)((n) * sizeof(t)) )




--------------------------------------------------
Dan Davison	
Committee on Evolutionary Biology, University of Chicago, USA
Field Museum of Natural History, Chicago, USA
http://home.uchicago.edu/~davison/




More information about the R-help mailing list