我正在学习使用 C 编程语言,并且可能遇到不兼容指针类型返回的问题。
我有一个包含两列的文本文件,R 将其作为 data.frame 读取。读完这篇文章后,我想在 C 中执行一个 .Call 函数,该函数将读取该 date.frame 并将返回 R 中变量的值,但是在尝试返回该值时会发生段错误。我找不到这个问题的解决方案,谁能帮助我?
文本文件的结构分为两列,如下例所示。
Q0045 YJL166W
Q0045 YDL085W
Q0045 YDR119W-A
这是读取 data.frame 的 C 代码。
#include <Rinternals.h>
#include <Rdefines.h>
#include <R.h>
#include <stdlib.h>
#include <stdio.h>
char **test(SEXP lst){
int i,elLength;
int len = length(lst);
SEXP col1, col2;
char ***target = malloc(sizeof(char **) *len);
col1 = VECTOR_ELT(lst, 0);
col2 = VECTOR_ELT(lst, 1);
elLength = length(col1);
target[0] = malloc(sizeof(char *) * elLength);
target[1] = malloc(sizeof(char *) * elLength);
for (i=0; i<elLength; i++) {
target[0][i] = CHAR(STRING_ELT(col1, i));
target[1][i] = CHAR(STRING_ELT(col2, i));
}
return target;
}
在此之后,我在终端中使用命令行创建 .so 文件:
R CMD SHLIB test.c
最后是 R 中读取文件并执行 .Call 的代码。
dyn.load("/home/lab/test.so")
fileR = data.frame(read.table("file.txt", sep = "\t", stringsAsFactors = FALSE))
fileFromC = .Call("test", fileR)
之后我从终端在 R 中运行时遇到错误:
*** caught segfault ***
address 0x310000c0, cause 'memory not mapped'
如果我只打印并返回 R_NilValue,则不会显示错误。但我需要将 C 进程返回到一个新变量。
最佳答案
malloc()
不是在 R 扩展中处理内存分配的正确方法。
请查看 R 文档的 6.1 Memory allocation 部分
6.1.1 Transient storage allocation
Here R will reclaim the memory at the end of the call to .C, .Call or .External. Use
char *R_alloc(size_t n, int size)
which allocates n units of size bytes each.
即您将此分配器用于要从 C API 中返回
的内存。
关于c - 使用 .Call 将值从 C 返回到 R 时出现段错误(不兼容的指针类型返回),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54484474/