- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有从 R 调用的 C 代码:
.C("giveProb",as.double(2),as.double(2),as.double(c(0,1,0,1,1,0,1,0)))
我想调用它很多次(数十万次或数百万次)(使用不同的第三个参数),当我将它放入 for 循环中大约 100 次时它工作正常,但超过此次数的任何操作都会崩溃
我感觉这是使用 R_alloc 的内存问题。我在 C 中分配了六个数组,例如:
newCoefArray = (double *)R_alloc(1,curSize * sizeof(double));
但是根据 R 手册“Writing R Extensions”:
This memory is taken from the heap, and released at the end of the .C, .Call or .External call.
我认为这意味着内存将在循环的每次迭代期间被释放。但在下一句中:
Users can also manage it, by noting the current position with a call to vmaxget and clearing memory allocated subsequently by a call to vmaxset. This is only recommended for experts.
作为一个远非专家的人,我希望这里有人能提供帮助。完整的 C 代码如下。
#include <R.h>
#include <Rmath.h>
#include <stdio.h>
void giveProb(double *k, double *q,double *order){
double curSize;
double tmpSize;
double *newCoefArray;
double *oldCoefArray;
double *newAArray;
double *oldAArray;
double *newBArray;
double *oldBArray;
int position=0;
long int factorial(int n){
if(n==0||n==1){
return(1);
}
int tmp=1,i=1;
while(i<=n){
tmp=tmp*i;
i++;
}
return(tmp);
}
void expander(double a, double b,double c,double d,double coeff){
double leadingTerm=beta(a,b);
int bb=b;
double index[bb], sumLeaders[bb];
for(int i=0;i<bb;i++){
index[i]=a+i;
sumLeaders[i]=factorial(a+b-1)/(factorial(index[i])*factorial(a+b-1-index[i]));
newCoefArray[i+position]=coeff*leadingTerm*sumLeaders[i];
newAArray[i+position]=index[i]+c+1;
newBArray[i+position]=a+b+d-index[i];
}
position=position+bb;
curSize=position;
}
void separator(double e, double f){
double a, b, coeff;
for(int i=0; i<tmpSize; i++){
coeff=oldCoefArray[i];
a=oldAArray[i];
b=oldBArray[i];
expander(a,b,e,f,coeff);
}
}
void condenser(){
tmpSize=0;
for(int i=1; i<curSize; i++){
for(int j=0; j<i; j++){
if(newAArray[j]==newAArray[i]){
newCoefArray[j]=newCoefArray[j]+newCoefArray[i];
newCoefArray[i]=0;
}
}
}
for(int i=0; i<curSize; i++){
tmpSize=tmpSize+(newCoefArray[i]!=0);
}
oldCoefArray =(double *) R_alloc(1,tmpSize * sizeof(double));
oldAArray = (double *)R_alloc(1,tmpSize * sizeof(double));
oldBArray = (double *)R_alloc(1,tmpSize * sizeof(double));
for(int i=0; i<tmpSize; i++){
oldCoefArray[i]=newCoefArray[i];
oldAArray[i]=newAArray[i];
oldBArray[i]=newBArray[i];
}
curSize=tmpSize;
}
long double coefficient=1;
for(int i=0;i<*k;i++){
coefficient=coefficient*factorial(*k)/(factorial(i)*factorial(*k-i-1));
}
for(int i=0;i<*q;i++){
coefficient=coefficient*factorial(*q)/(factorial(i)*factorial(*q-i-1));
}
double numObs=*k+*q;
double out=0;
curSize=order[1]+1;
newCoefArray = (double *)R_alloc(1,curSize * sizeof(double));
newAArray = (double *)R_alloc(1,curSize * sizeof(double));
newBArray = (double *)R_alloc(1,curSize * sizeof(double));
expander(order[0]+1,order[1]+1,order[2],order[3],coefficient);
oldCoefArray = (double *)R_alloc(1,curSize * sizeof(double));
oldAArray = (double *)R_alloc(1,curSize * sizeof(double));
oldBArray = (double *)R_alloc(1,curSize * sizeof(double));
for(int i=0;i<curSize; i++){
oldCoefArray[i]=newCoefArray[i];
oldAArray[i]=newAArray[i];
oldBArray[i]=newBArray[i];
}
for(int i=4;i<2*numObs;i+=2){
position=0;
tmpSize=curSize;
separator(order[i],order[i+1]);
condenser();
}
position=0;
for(int i=0;i<curSize;i++){
out=out+newCoefArray[i]*beta(newAArray[i],newBArray[i]);
}
*k=out;
}
更新:使用下面评论中的建议,我得到以下信息(哪一种证实了我之前的想法,对吧?):
R -d valgrind -f test_script.R ==11131== Memcheck,内存错误检测器 ==11131== 版权所有 (C) 2002-2010 和 GNU GPL'd,Julian Seward 等人。 ==11131== 使用 Valgrind-3.6.0 和 LibVEX;使用 -h 重新运行以获得版权信息 ==11131== 命令:/usr/lib64/R/bin/exec/R -f test_script.R ==11131==
R version 2.15.1 (2012-06-22) -- "Roasted Marshmallows"
Copyright (C) 2012 The R Foundation for Statistical Computing
ISBN 3-900051-07-0
Platform: x86_64-redhat-linux-gnu (64-bit)
R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.
==11131== Conditional jump or move depends on uninitialised value(s)
==11131== at 0x3A6A685F80: __GI___strcasecmp_l (in /lib64/libc-2.12.so)
==11131== by 0x3A6A61FF24: __gconv_open (in /lib64/libc-2.12.so)
==11131== by 0x3A6A62D3B7: _nl_find_msg (in /lib64/libc-2.12.so)
==11131== by 0x3A6A62DB83: __dcigettext (in /lib64/libc-2.12.so)
==11131== by 0x3A6C3BD2DF: ??? (in /usr/lib64/R/lib/libR.so)
==11131== by 0x3A6C313FC8: setup_Rmainloop (in /usr/lib64/R/lib/libR.so)
==11131== by 0x3A6C315278: Rf_mainloop (in /usr/lib64/R/lib/libR.so)
==11131== by 0x40084A: main (in /usr/lib64/R/bin/exec/R)
==11131==
==11131== Use of uninitialised value of size 8
==11131== at 0x3A6A6863A4: __GI___strcasecmp_l (in /lib64/libc-2.12.so)
==11131== by 0x3A6A61FF24: __gconv_open (in /lib64/libc-2.12.so)
==11131== by 0x3A6A62D3B7: _nl_find_msg (in /lib64/libc-2.12.so)
==11131== by 0x3A6A62DB83: __dcigettext (in /lib64/libc-2.12.so)
==11131== by 0x3A6C3BD2DF: ??? (in /usr/lib64/R/lib/libR.so)
==11131== by 0x3A6C313FC8: setup_Rmainloop (in /usr/lib64/R/lib/libR.so)
==11131== by 0x3A6C315278: Rf_mainloop (in /usr/lib64/R/lib/libR.so)
==11131== by 0x40084A: main (in /usr/lib64/R/bin/exec/R)
==11131==
==11131== Use of uninitialised value of size 8
==11131== at 0x3A6A6863A8: __GI___strcasecmp_l (in /lib64/libc-2.12.so)
==11131== by 0x3A6A61FF24: __gconv_open (in /lib64/libc-2.12.so)
==11131== by 0x3A6A62D3B7: _nl_find_msg (in /lib64/libc-2.12.so)
==11131== by 0x3A6A62DB83: __dcigettext (in /lib64/libc-2.12.so)
==11131== by 0x3A6C3BD2DF: ??? (in /usr/lib64/R/lib/libR.so)
==11131== by 0x3A6C313FC8: setup_Rmainloop (in /usr/lib64/R/lib/libR.so)
==11131== by 0x3A6C315278: Rf_mainloop (in /usr/lib64/R/lib/libR.so)
==11131== by 0x40084A: main (in /usr/lib64/R/bin/exec/R)
==11131==
Natural language support but running in an English locale
R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.
Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.
[Previously saved workspace restored]
> dyn.load("SchWolfenew.so")
> for(i in 1:1000){
+ .C("giveProb",as.double(2),as.double(2),as.double(c(0,1,0,1,1,0,1,0)))
+ }
==29371== Invalid read of size 1
==29371== at 0x3A6C31A2E9: ??? (in /usr/lib64/R/lib/libR.so)
==29371== by 0x3A6C31CBC9: Rf_cons (in /usr/lib64/R/lib/libR.so)
==29371== by 0x3A6C2D2B74: ??? (in /usr/lib64/R/lib/libR.so)
==29371== by 0x3A6C2DD121: Rf_eval (in /usr/lib64/R/lib/libR.so)
==29371== by 0x3A6C2DF830: Rf_applyClosure (in /usr/lib64/R/lib/libR.so)
==29371== by 0x3A6C32B828: Rf_usemethod (in /usr/lib64/R/lib/libR.so)
==29371== by 0x3A6C32BAE7: ??? (in /usr/lib64/R/lib/libR.so)
==29371== by 0x3A6C2D290B: ??? (in /usr/lib64/R/lib/libR.so)
==29371== by 0x3A6C2DD121: Rf_eval (in /usr/lib64/R/lib/libR.so)
==29371== by 0x3A6C2DF830: Rf_applyClosure (in /usr/lib64/R/lib/libR.so)
==29371== by 0x3A6C2DD3F7: Rf_eval (in /usr/lib64/R/lib/libR.so)
==29371== by 0x3A6C2DEF4F: ??? (in /usr/lib64/R/lib/libR.so)
==29371== Address 0x3ff0000000000003 is not stack'd, malloc'd or (recently) free'd
==29371==
*** caught segfault ***
address (nil), cause 'unknown'
==29371== Invalid read of size 1
==29371== at 0x3A6C31AF0B: ??? (in /usr/lib64/R/lib/libR.so)
==29371== by 0x3A6C31CBC9: Rf_cons (in /usr/lib64/R/lib/libR.so)
==29371== by 0x3A6C31CC71: Rf_allocList (in /usr/lib64/R/lib/libR.so)
==29371== by 0x3A6C2C8CD4: R_GetTraceback (in /usr/lib64/R/lib/libR.so)
==29371== by 0x3A6C313472: ??? (in /usr/lib64/R/lib/libR.so)
==29371== by 0x3A6B20F4FF: ??? (in /lib64/libpthread-2.12.so)
==29371== by 0x3A6C31A2E8: ??? (in /usr/lib64/R/lib/libR.so)
==29371== by 0x3A6C31CBC9: Rf_cons (in /usr/lib64/R/lib/libR.so)
==29371== by 0x3A6C2D2B74: ??? (in /usr/lib64/R/lib/libR.so)
==29371== by 0x3A6C2DD121: Rf_eval (in /usr/lib64/R/lib/libR.so)
==29371== by 0x3A6C2DF830: Rf_applyClosure (in /usr/lib64/R/lib/libR.so)
==29371== by 0x3A6C32B828: Rf_usemethod (in /usr/lib64/R/lib/libR.so)
==29371== Address 0x4020000000000003 is not stack'd, malloc'd or (recently) free'd
==29371==
==29371==
==29371== Process terminating with default action of signal 11 (SIGSEGV)
==29371== General Protection Fault
==29371== at 0x3A6C31AF0B: ??? (in /usr/lib64/R/lib/libR.so)
==29371== by 0x3A6C31CBC9: Rf_cons (in /usr/lib64/R/lib/libR.so)
==29371== by 0x3A6C31CC71: Rf_allocList (in /usr/lib64/R/lib/libR.so)
==29371== by 0x3A6C2C8CD4: R_GetTraceback (in /usr/lib64/R/lib/libR.so)
==29371== by 0x3A6C313472: ??? (in /usr/lib64/R/lib/libR.so)
==29371== by 0x3A6B20F4FF: ??? (in /lib64/libpthread-2.12.so)
==29371== by 0x3A6C31A2E8: ??? (in /usr/lib64/R/lib/libR.so)
==29371== by 0x3A6C31CBC9: Rf_cons (in /usr/lib64/R/lib/libR.so)
==29371== by 0x3A6C2D2B74: ??? (in /usr/lib64/R/lib/libR.so)
==29371== by 0x3A6C2DD121: Rf_eval (in /usr/lib64/R/lib/libR.so)
==29371== by 0x3A6C2DF830: Rf_applyClosure (in /usr/lib64/R/lib/libR.so)
==29371== by 0x3A6C32B828: Rf_usemethod (in /usr/lib64/R/lib/libR.so)
==29371==
==29371== HEAP SUMMARY:
==29371== in use at exit: 29,307,724 bytes in 12,896 blocks
==29371== total heap usage: 28,845 allocs, 15,949 frees, 48,495,252 bytes allocated
==29371==
==29371== LEAK SUMMARY:
==29371== definitely lost: 0 bytes in 0 blocks
==29371== indirectly lost: 0 bytes in 0 blocks
==29371== possibly lost: 0 bytes in 0 blocks
==29371== still reachable: 29,307,724 bytes in 12,896 blocks
==29371== suppressed: 0 bytes in 0 blocks
==29371== Rerun with --leak-check=full to see details of leaked memory
==29371==
==29371== For counts of detected and suppressed errors, rerun with: -v
==29371== Use --track-origins=yes to see where uninitialised values come from
==29371== ERROR SUMMARY: 5 errors from 5 contexts (suppressed: 21 from 9)
Segmentation fault (core dumped)
最佳答案
我将你的 C 代码放在文件 memory.c 中,然后运行 R CMD SHLIB memory.c。这是我的测试脚本
dyn.load("/tmp/memory.so")
set.seed(123L)
while (TRUE)
.C("giveProb",as.double(2),as.double(2), sample(c(0, 1), 8, TRUE))
R -d valgrind -f test_script.R 说
> dyn.load("/tmp/memory.so")
> set.seed(123L)
> while (TRUE)
+ .C("giveProb",as.double(2),as.double(2), sample(c(0, 1), 8, TRUE))
==3461== Invalid write of size 8
==3461== at 0xBF29D78: expander.4631 (memory.c:35)
==3461== by 0xBF29EBB: separator.4643 (memory.c:49)
==3461== by 0xBF29AAB: giveProb (memory.c:108)
==3461== by 0x4EEAF49: do_dotCode (dotcode.c:1689)
==3461== by 0x4F1F4E2: Rf_eval (eval.c:493)
==3461== by 0x4F218F6: do_for (eval.c:1310)
==3461== by 0x4F1F2E8: Rf_eval (eval.c:467)
==3461== by 0x4F6B5FB: Rf_ReplIteration (main.c:256)
==3461== by 0x4F6B7B2: R_ReplConsole (main.c:305)
==3461== by 0x4F6D022: run_Rmainloop (main.c:987)
==3461== by 0x4F6D037: Rf_mainloop (main.c:994)
==3461== by 0x400845: main (Rmain.c:32)
(还有更多)。 memory.c 第 35 行是
newCoefArray[i+position]=coeff*leadingTerm*sumLeaders[i];
所以 i + position 大于分配的数组。
关于c - 在 C 中使用 R_alloc,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12084404/
#include using namespace std; class C{ private: int value; public: C(){ value = 0;
这个问题已经有答案了: What is the difference between char a[] = ?string?; and char *p = ?string?;? (8 个回答) 已关闭
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 7 年前。 此帖子已于 8 个月
除了调试之外,是否有任何针对 c、c++ 或 c# 的测试工具,其工作原理类似于将独立函数复制粘贴到某个文本框,然后在其他文本框中输入参数? 最佳答案 也许您会考虑单元测试。我推荐你谷歌测试和谷歌模拟
我想在第二台显示器中移动一个窗口 (HWND)。问题是我尝试了很多方法,例如将分辨率加倍或输入负值,但它永远无法将窗口放在我的第二台显示器上。 关于如何在 C/C++/c# 中执行此操作的任何线索 最
我正在寻找 C/C++/C## 中不同类型 DES 的现有实现。我的运行平台是Windows XP/Vista/7。 我正在尝试编写一个 C# 程序,它将使用 DES 算法进行加密和解密。我需要一些实
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
有没有办法强制将另一个 窗口置于顶部? 不是应用程序的窗口,而是另一个已经在系统上运行的窗口。 (Windows, C/C++/C#) 最佳答案 SetWindowPos(that_window_ha
假设您可以在 C/C++ 或 Csharp 之间做出选择,并且您打算在 Windows 和 Linux 服务器上运行同一服务器的多个实例,那么构建套接字服务器应用程序的最明智选择是什么? 最佳答案 如
你们能告诉我它们之间的区别吗? 顺便问一下,有什么叫C++库或C库的吗? 最佳答案 C++ 标准库 和 C 标准库 是 C++ 和 C 标准定义的库,提供给 C++ 和 C 程序使用。那是那些词的共同
下面的测试代码,我将输出信息放在注释中。我使用的是 gcc 4.8.5 和 Centos 7.2。 #include #include class C { public:
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
我的客户将使用名为 annoucement 的结构/类与客户通信。我想我会用 C++ 编写服务器。会有很多不同的类继承annoucement。我的问题是通过网络将这些类发送给客户端 我想也许我应该使用
我在 C# 中有以下函数: public Matrix ConcatDescriptors(IList> descriptors) { int cols = descriptors[0].Co
我有一个项目要编写一个函数来对某些数据执行某些操作。我可以用 C/C++ 编写代码,但我不想与雇主共享该函数的代码。相反,我只想让他有权在他自己的代码中调用该函数。是否可以?我想到了这两种方法 - 在
我使用的是编写糟糕的第 3 方 (C/C++) Api。我从托管代码(C++/CLI)中使用它。有时会出现“访问冲突错误”。这使整个应用程序崩溃。我知道我无法处理这些错误[如果指针访问非法内存位置等,
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 7 年前。
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是偏离主题的,因为
我有一些 C 代码,将使用 P/Invoke 从 C# 调用。我正在尝试为这个 C 函数定义一个 C# 等效项。 SomeData* DoSomething(); struct SomeData {
这个问题已经有答案了: Why are these constructs using pre and post-increment undefined behavior? (14 个回答) 已关闭 6
我是一名优秀的程序员,十分优秀!