I am testing a call to a C function from R, using .C interface. The test
consists in passing a numeric vector to the C function with no entries,
dynamically allocates n positions, makes attributions and return the vector
to R.

I'm using Calloc from R.h. The prototype of the function is

type* Calloc(size_t n, type)

as noted in Writing R Extensions.

The problem is that I don't get the new vector with the allocated positions
in R. The vector continues to have no entries.

*The code in R*

fooR <- function(x) {
  if (!is.numeric(x))
    stop("argument x must be numeric")
  out <- .C("foo",
            x=as.double(x))
  return(out$x)}

x <- numeric()

result <- myfooR(x)

*The function in C*

#include <R.h>
void myfooRealloc(double *x){
  int i, n;

  n = 4;
  x = Calloc(n, double);

  for (i = 0; i < n; i++) {
    x[i] = i;
    printf("%f\n", x[i]); //just to check
  }}

The question is: Can .C inteface handle with such memory allocation?

	[[alternative HTML version deleted]]


