R调用的C代码不断崩溃

R调用的C代码不断崩溃,c,r,crash,C,R,Crash,下面是我写的C代码的一部分。函数foo将在R中调用。该代码不断导致R崩溃,我将问题缩小到这个outer()函数,该函数用于计算外部和或差。请注意注释掉的部分:如果我不注释掉它,则如果每个数组都包含(比如)1000多个数据点,则函数将导致R崩溃。如果我把它注释掉,我就可以毫无问题地计算更长数组的外部和/差(例如,每个数组超过100000个数据点)。我想知道问题是什么。。。谢谢大家! #include <R.h> #include <Rmath.h> #include <

下面是我写的C代码的一部分。函数
foo
将在R中调用。该代码不断导致R崩溃,我将问题缩小到这个
outer()
函数,该函数用于计算外部和或差。请注意注释掉的部分:如果我不注释掉它,则如果每个数组都包含(比如)1000多个数据点,则函数将导致R崩溃。如果我把它注释掉,我就可以毫无问题地计算更长数组的外部和/差(例如,每个数组超过100000个数据点)。我想知道问题是什么。。。谢谢大家!

#include <R.h>
#include <Rmath.h>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>

void outer(double *x1, double *x2, int *n, int operation, double *output){
int i, j;
if(operation==1){
    for(i=0; i<*n; i++){
        for(j=0; j<*n; j++){
            output[(*n)*i+j]=x1[j]+x2[i];
        }
    }
} else if(operation==2){
    for(i=0; i<*n; i++){
        for(j=0; j<*n; j++){
            output[(*n)*i+j]=x1[j]-x2[i];
            //Rprintf("%d ", (*n)*i+j); //<-----------HERE
        }
    }
} 
}


void foo(double *x, double *y, int *npred, int *nsamp){
int oper=2;
double xouter[*nsamp], youter[*nsamp];
double outer_temp_x[(*nsamp)*(*nsamp)], outer_temp_y[(*nsamp)*(*nsamp)];

outer(x, x, nsamp, oper, &outer_temp_x[0]);
outer(y, y, nsamp, oper, &outer_temp_y[0]);

}

我认为这是过度运行的堆栈和造成的麻烦

试试这个:

void foo(double *x, double *y, int *npred, int *nsamp){
  int oper=2;
  double xouter[*nsamp], youter[*nsamp];

  // The prior code allocated on the stack.  Here, we make a pair of calls
  // to 'malloc' to allocate memory for the arrays.  This gets memory from
  // the heap.  The stack is fairly limited, but the heap is huge.
  // 'malloc' returns a pointer to the allocated memory.

  double* outer_temp_x=malloc(sizeof(double)*(*nsamp)*(*nsamp));
  double* outer_temp_y=malloc(sizeof(double)*(*nsamp)*(*nsamp));

  outer(x, x, nsamp, oper, &outer_temp_x[0]);
  outer(y, y, nsamp, oper, &outer_temp_y[0]);

  // The downside of allocating on the heap, is that you must release the
  // memory at some point.  Otherwise you have what's called a "memory leak."
  // 'free' is the function to free the memory, and it is called on the
  // pointer value returned by 'malloc'.

  free(outer_temp_x);
  free(outer_temp_y);
}

这对我来说是一个错误,用
Rprintf
注释掉了。真奇怪。我尝试了很多次,当Rprintf被注释掉时,它并没有使R崩溃。让我再试一次,再试一次。没问题。真的很奇怪。@MatthewLundberg:当你的数组崩溃时,它的大小是多少?如你的例子中的R?10000。你只是添加了那些行,还是也添加了对
malloc
?你能解释一下
double*outer\u temp\u x=malloc(sizeof(double)*(*nsamp)*(*nsamp))
double*outer\u temp\u y=malloc sizeof(double)*(*nsamp)*(*nsamp))
?你的代码成功了。谢谢指向使用
R\u alloc
进行动态内存分配(返回到R时自动检索,无需
free
)和
Calloc
/
free
,以实现跨平台的一致性。谢谢,@MartinMorgan。在这里,这些并没有被返回(这些显然是临时的),所以这不是一个问题。
void foo(double *x, double *y, int *npred, int *nsamp){
  int oper=2;
  double xouter[*nsamp], youter[*nsamp];

  // The prior code allocated on the stack.  Here, we make a pair of calls
  // to 'malloc' to allocate memory for the arrays.  This gets memory from
  // the heap.  The stack is fairly limited, but the heap is huge.
  // 'malloc' returns a pointer to the allocated memory.

  double* outer_temp_x=malloc(sizeof(double)*(*nsamp)*(*nsamp));
  double* outer_temp_y=malloc(sizeof(double)*(*nsamp)*(*nsamp));

  outer(x, x, nsamp, oper, &outer_temp_x[0]);
  outer(y, y, nsamp, oper, &outer_temp_y[0]);

  // The downside of allocating on the heap, is that you must release the
  // memory at some point.  Otherwise you have what's called a "memory leak."
  // 'free' is the function to free the memory, and it is called on the
  // pointer value returned by 'malloc'.

  free(outer_temp_x);
  free(outer_temp_y);
}