2013-05-07 3 views
6

Unten ist Teil eines C-Code, den ich schrieb. Die Funktion foo soll in R aufgerufen werden. Der Code führt dazu, dass R abstürzt, und ich beschränkte das Problem auf diese outer()-Funktion, die zur Berechnung der äußeren Summe oder Differenz verwendet wird. Beachten Sie den auskommentierten Teil: Wenn ich es nicht auskommentiere, führt die Funktion R zum Absturz , wenn jedes der Arrays, sagen wir, über 1000 Datenpunkte enthält. Wenn ich es auskommentiere, kann ich die äußere Summe/Differenz für wesentlich längere Arrays ohne Probleme berechnen (z. B. über 100000 Datenpunkte pro Array). Ich frage mich, was das Problem ist ... Danke!C-Code von R gerufen stürzt

#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]); 

} 

// Nach dem Kompilieren des Codes verwende ich den Code unten in R die Funktion aufzurufen:

dyn.load("foo.so") 
x=as.matrix(rnorm(10000)) 
y=rlnorm(10000) 

invisible(.C("foo", 
      x=as.double(as.vector(x)), 
      y=as.double(y), 
      npred=as.integer(ncol(x)), 
      nsamp=as.integer(length(y)) 
     ) 
+0

Dieser stürzt R für mich, mit der 'Rprintf' out kommentiert. –

+0

Uh. Das ist wirklich komisch. Ich habe es viele Male ausprobiert und es kam nicht zum Absturz von R, als Rprintf auskommentiert wurde. Lass es mich nochmal probieren. – Alex

+0

Habe es einfach nochmal probiert. Es funktionierte ohne Probleme. Sehr seltsam. – Alex

Antwort

7

Ich denke, es wird der Stapel overunning und verursacht Probleme.

Versuchen Sie folgendes:

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); 
} 
+0

Das Hinzufügen von 'free (outer_temp_x)' und 'free (outer_temp_y)' stürzte mein R .. – Alex

+0

Haben Sie nur diese Zeilen hinzugefügt, oder fügen Sie auch die Aufrufe von 'malloc' hinzu? –

+0

Ups, mein Schlechter! Ich habe die anderen Änderungen nicht bemerkt. War für eine Sekunde abgelenkt .. – Alex

Verwandte Themen