[Rd] malloc/calloc/strdup and R's aequivalents

oliver oliver at first.in-berlin.de
Sun Mar 18 03:30:52 CET 2012


when looking at "Writing R Extensions"
with mem-allocation in mind, I wondered,
which functions to use to substitute
malloc(), calloc(), realloc() and strdup() and free().

It looked like Calloc() or R_Calloc() might be useful for
some of my tasks, but when trying to use R_Calloc() for example,
I got some error messages which I don't see where they are coming from.

Maybe I just have forgotten to includ ethe right header file?

So, how to use Calloc() / R_Calloc()?
What is the prototype of this function/macro?

And: what if I just want to easily substitute strdup()/free()?

mkChar seems not to be the right thing, because, what I found in the
above mentioned documentation, says,m that a C-string is fed in, but
CHARSXP is what I get as result.

What I try to do, is, to pick some code and port it to R.
If I need to rewrite a lot of stuff, just because I need
to adapt to R, this would be more effort than if it would be possible
just to have some functions, that could make porting easier.

For example, if I want to resuse a function, that opens a file
and needs a   char* stringname
then it would add more effort if I need to go via CHARSXP or so.

I would need to rewrite many pieces of the code.

(Should I implement my own kind of R_str_alloc() ?)

My main problem at the moemnt is, that I can use R_alloc()
but when I try to use Calloc() or R_Calloc() I can't compile.

So, what header-files are necessary, and what are the prototypes
of these functions?

If nothing of that stuff works, I would need to use the original
calloc() / free() functions, which are deprecated in the above
mentioned manual.


P.S.: Also I wonder... R-alloc() does not need a free()-like call?
      Is it freed automaticlaly?
      And at which time?
      In the docs is mentioned, at the end of the .C/.Call/.External call.
      Does this mean, this is freed/reclaimed automatically, when returning
      from the C-world to the R-world?
      At (after) return of the extension's code?

More information about the R-devel mailing list