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/
On Fri, 5 Dec 2003, Dan Davison wrote:> 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.Yes. You can't do this with .C, so use .Call instead.> 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:A size_t is a non-negative integer, and it may not be an int (it could well be larger on a 64-bit machine). Calloc and Realloc are macros, and they inform the standard C coercion rules, just as those declarations imply. -- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
Dan Davison wrote:> 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?You should probably look into using .Call instead. There's a bit of learning overhead, but the payoff is that it's much more flexible. -- Ross Ihaka Email: ihaka at stat.auckland.ac.nz Department of Statistics Phone: (64-9) 373-7599 x 85054 University of Auckland Fax: (64-9) 373-7018 Private Bag 92019, Auckland New Zealand
Dan Davison <davison at uchicago.edu> writes:> 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.For something like this it is better to use the .Call interface than to use the .C interface. See the description in "Writing R Extensions". If that documentation is insufficient then I can send you copies of the slides from our short course on "Advanced Programming in R" from DSC-2003.