【发布时间】:2014-05-23 03:21:19
【问题描述】:
我正在使用 .C 接口测试从 R 对 C 函数的调用。测试包括将一个数字向量传递给没有条目的 C 函数,动态分配 n 个位置,进行归因并将向量返回给 R。
我正在使用 R.h. 的 Calloc。该函数的原型是
type* Calloc(size_t n, type)
如编写 R 扩展中所述。
问题是我没有得到带有 R 中分配位置的新向量。向量仍然没有条目。
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 <- fooR(x)
C语言中的函数
#include <R.h>
void foo(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
}
}
问题是:.C 接口可以处理这样的内存分配吗?
【问题讨论】:
标签: c r memory-management vector dynamic-allocation