在一个在R包中调用的C函数中,我需要对一些数字进行排序。 为了与R做的一致,我想调用R使用的排序算法/函数,所以R_orderVector()。 我在R_orderVector()的调用时完全得到了一个分段错误。 下面是一个最小的工作示例(“最小工作包”的文件),它再现了分段错误。 我究竟做错了什么?
### DESCRIPTION ################################################################ Package: foo Version: 0.0-1 Encoding: UTF-8 Title: Sorting from C via R's R_orderVector() Description: See title Authors@R: c(person(given = "Foo", family = "Bar", role = c("aut", "cre"), email = "foo@bar.com")) Author: Foo Bar [aut, cre] Maintainer: Foo Bar Depends: R (>= 3.0.0) Imports: Suggests: Enhances: License: GPL-2 | GPL-3 NeedsCompilation: yes Repository: CRAN Date/Publication: 2014-03-25 15:26:50 ### NAMESPACE ################################################################## useDynLib(foo, .registration = TRUE) export("myRsort") ### ./R/mySort.R ############################################################### myRsort <- function(x) { stopifnot(is.numeric(x), length(x) <= 64) myRsort_ <- NULL # to avoid "myRsort_: no visible binding for global variable 'myRsort_'" .Call("myRsort_", x) } ### ./man/myRsort.Rd ########################################################### name{myRsort} alias{myRsort} title{Using R's Sorting Algorithm from C} description{ R's sorting algorithm is called from C. } usage{ myRsort(x) } arguments{ item{x}{vector} } value{ vector } author{Marius Hofert} examples{ set.seed(271) x <- runif(10) myRsort(x) } keyword{utilities} ### ./src/init.c ############################################################### #include #include #include #include "myRsort.h" static const R_CallMethodDef callMethods[] = { {"myRsort_", (DL_FUNC) &myRsort_, 1}, {NULL, NULL, 0} }; void R_init_foo(DllInfo *dll) { R_useDynamicSymbols(dll, FALSE); R_registerRoutines(dll, NULL, callMethods, NULL, NULL); /* s. WRE (2015, Section 5.4) */ } ### ./src/myRsort.c ############################################################ #include "myRsort.h" void myRsort_aux(double *x, int n, double *res) { int *ind; /* pointer to vector of indices as required for order (permutation of 0:(n-1)) */ ind = (int *) R_alloc(n, sizeof(int)); SEXP xsexp = PROTECT(allocVector(REALSXP, n)); /* turn x into SEXP */ double *xsexp_ = REAL(xsexp); /* pointer */ R_orderVector(ind, n, xsexp, TRUE, TRUE); /* nalast (use same default as order()); decreasing=TRUE */ /* the last line generates a seg-fault */ int i; for(i=0; i<n; i++) res[i] = x[ind[i]]; } SEXP myRsort_(SEXP x) { double *x_ = REAL(x); /* pointer to n-vector */ int n = LENGTH(x); /* length n */ int maxlen = 64; /* vector can be at most of length 64 here */ SEXP res = PROTECT(allocVector(REALSXP, maxlen)); /* result */ double *res_ = REAL(res); /* pointer to the (sorted) result */ myRsort_aux(x_, n, res_); UNPROTECT(1); return res; } ### ./src/myRsort.h ############################################################ #ifndef myRsort_H #define myRsort_H #include #include #include void myRsort_aux(double *x, int n, double *res); SEXP myRsort_(SEXP x); #endif
以上就是c/c++开发分享如何在C中调用R的顺序函数(通过R_orderVector())?相关内容,想了解更多C/C++开发(异常处理)及C/C++游戏开发关注计算机技术网(www.ctvol.com)!)。
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/c-cdevelopment/522867.html