[Rd] malloc/calloc/strdup and R's aequivalents
oliver at first.in-berlin.de
Sun Mar 18 06:16:11 CET 2012
On Sat, Mar 17, 2012 at 10:08:05PM -0500, Dirk Eddelbuettel wrote:
> On 18 March 2012 at 03:30, oliver wrote:
> | Hello,
> | 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?
> Maybe, but we can't tell as you didn't post a reproducible example. Here is
> one, and I turned verbose on to give you the (default) headers:
It was not a missing header-file.
> R> library(inline)
> R> f <- cfunction(signature(), verbose=TRUE, body='
> + double *p = Calloc(5, double);
That line cleared the issue.
Thank you for providing an example.
When reading in the documentation I was not sure,
how to interpret "type" in setion 6.1.2.
It was meant as the name of the type, and that's, why my
sizeof() stuff created the problem.
I tried around and then saw, that the name of the type
is accepted, not only with typical base-types of C, but
it also eats the names of my structs/typedefed structs.
OK, so this is the solution... Calloc()'s snd arg is
a name of a type. So I assume it will internally use sizeof.
As this is rather untypical to the C-ish thinking,
and also not aequivalent to the calloc() which is
substituted by calloc(),
I think, it would be good, if this could be explained
clearer / more precise in the Writing Extensions document.
For example the R_alloc() prototype is like the one from calloc(). (*)
But Calloc() is different here.
My first impression was, that it's rather an accident / typo,
what was described in the docs, because it was a bit unusual.
The usage with names of types is nice; but explaining,
that this is not a typo, would be good too.
P.S.: (*) Not quite right: calloc() returns void*,
but R_alloc() is mentioned to return char*.
Here I'm also not sure if this is a typo,
and one reason, why I thought the "type"
in the following section might also be one.
More information about the R-devel