[R] allocating memory in a C module (fwd)
Thomas Lumley
tlumley at u.washington.edu
Sat Oct 4 02:10:47 CEST 2003
I pressed ^X in the wrong window. Here's the right code:
Here's some code that does a pointless example
In C:
#include "Rinternals.h"
/* function takes no arguments and returns an object */
SEXP pie(){
int n;
SEXP alist, avector;
/* work out how long a list to return */
n = 2;
/* create the list */
PROTECT(alist = allocVector(VECSXP, n));
/* create the first vector and populate it */
n = 4;
PROTECT(avector = allocVector(INTSXP, n));
INTEGER(avector)[0] = 3;
INTEGER(avector)[1] = 1;
INTEGER(avector)[2] = 4;
INTEGER(avector)[3] = 1;
/* stick it into the list */
SET_VECTOR_ELT(alist, 0, avector);
UNPROTECT(1);
/* create the second vector and populate it */
n = 4;
PROTECT(avector = allocVector(INTSXP, n));
INTEGER(avector)[0] = 5;
INTEGER(avector)[1] = 9;
INTEGER(avector)[2] = 2;
INTEGER(avector)[3] = 7;
/* stick it into the list */
SET_VECTOR_ELT(alist, 1, avector);
UNPROTECT(1);
UNPROTECT(1); /* alist*/
return alist;
}
in R
dyn.load("pie.so")
.Call("pie")
-thomas
Thomas Lumley Assoc. Professor, Biostatistics
tlumley at u.washington.edu University of Washington, Seattle
More information about the R-help
mailing list