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

From: Dirk Eddelbuettel <edd_at_debian.org>
Date: Sat, 17 Mar 2012 22:08:05 -0500

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:

R> library(inline)
R> 
R> f <- cfunction(signature(), verbose=TRUE, body='
+    double *p = Calloc(5, double);
+    Free(p);

+ return R_NilValue;
+ ')
Compilation argument:
 /usr/lib/R/bin/R CMD SHLIB file25df49ab1ccf.cpp 2> file25df49ab1ccf.cpp.err.txt ccache g++-4.6 -I/usr/share/R/include -fpic -g0 -O3 -Wall -pipe -Wno-unused -pedantic -c file25df49ab1ccf.cpp -o file25df49ab1ccf.o g++ -shared -o file25df49ab1ccf.so file25df49ab1ccf.o -L/usr/lib/R/lib -lR Program source:
  1. #include <R.h>
  2. #include <Rdefines.h>
  3. #include <R_ext/Error.h> 4: 5:
  4. extern "C" {
  5. SEXP file25df49ab1ccf ( );
  6. } 9:
  7. SEXP file25df49ab1ccf ( ) { 11:
  8. double *p = Calloc(5, double);
  9. Free(p);
  10. return R_NilValue; 15:
  11. warning("your C program does not return anything!");
  12. return R_NilValue;
  13. } R> R> str(f()) NULL R>

Dirk

-- 
"Outside of a dog, a book is a man's best friend. Inside of a dog, it is too
dark to read." -- Groucho Marx

______________________________________________
R-devel_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Received on Sun 18 Mar 2012 - 03:10:11 GMT

This quarter's messages: by month, or sorted: [ by date ] [ by thread ] [ by subject ] [ by author ]

All messages

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Sun 18 Mar 2012 - 10:30:30 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-devel. Please read the posting guide before posting to the list.

list of date sections of archive