[R] S_alloc or Calloc for return value
Dirk Eddelbuettel
edd at debian.org
Tue Jul 21 02:44:27 CEST 2009
On 20 July 2009 at 16:06, Dan Kelley wrote:
| I am trying to write a C function to create a vector of integers that can be
| used by the R calling function. I do not know the size of the vector in the
| R calling function. (Well, actually, I have an upper limit on the size, but
| that is so large that R cannot allocate it. What I'm doing in the function
| is to do a sieving procedure, and the result will be small enough to fit
| into my machine's memory.)
|
| My reading of sections 6.1.1 and 6.1.2 of the guide on writing R extensions
| has left me a bit confused. Under R_alloc, I read "R will reclaim the
| memory at the end of the call" which seems to suggest the storage won't be
| made available afterwards, so that's not what I should use. As for the
| alterntaive, under Calloc, I read "This memory lasts until freed by the
| user", and it's clear that this is to be done within the C function in
| question.
|
| Summary question: how can I allocate memory withing a C function, making it
| available to an R function that calls the C function?
You want R_alloc(). Here, "end of the call" is the call of the R function
that calls your C function. This is what you want---the data will be
available for the caller of your C code.
FWIW R_alloc() is also the only allocation function used in Rcpp (which
provides an interface between R and C++).
Hth, Dirk
--
Three out of two people have difficulties with fractions.
More information about the R-help
mailing list