C++ 'Runtime error time: 0 memory: 3452 signal:11'

C++ 'Runtime error time: 0 memory: 3452 signal:11'

我是 C++ 编程的新手,我正在做一个项目。

其中一个核心函数应该检查一个数组,以确定它是否在 N sigma 内与矩阵中包含的其他数组兼容,我 post 下面的代码。

#include <iostream>
#include <cstdlib>
#include <cmath>
using namespace std;

void confronto(double*, double**, double**, double*, int*, int, int, int*);

double rnd(){return double(rand())/RAND_MAX;};

/*argomenti funzione, in ordine:
spettro misurato, database dei picchi, database delle dev. std, vettore dove 
metteremo tutti gli errori sulle parole, un vettore di 1 della stessa lunghezza 

delle parole (lo uso come booleano),
#parole, #campioni delle parole, classifica delle parole papabili

La funzione NON RITORNA VALORI, ma agisce direttamente su vettori e valori 

passati
dal programma. dovendo agire su vettori mi è sembrata la cosa più comoda.
Oltre agli input quindi le uniche var che ci interessano di più sono best ed 

err:
la prima è un vettore che restituisce gli indici corrispondenti alle parole che
hanno passato il test delle N deviazioni standard in ordine crescente di errore.
La seconda ci fornisce per ogni parola l'errore associato in unità arbitrarie, 
che può essere usato come stima della bontà della identificazione.*/
void confronto(double *spettro, double* datab[5], double* datasig[5], double err[], 
                                int accettabile[], int nwords, int size, int best[]){
    //numero max dev std
    int N= 2;
    //numero max picchi "ciccati"
    int failmax = 2;
    //contatore per i fallimenti
    int fail;


    for (int i =0; i<nwords; i++){
        fail = 0;
        for (int j=0; j<size; j++){
            err[i]+= abs(spettro[j] - datab[i][j]);
            if((abs(spettro[j]-datab[i][j])/datasig[i][j])>N){
                fail++;
                if(fail>failmax){accettabile[i]=0;};
            };
        };

    };

    //i prossimi due double sono "segnaposto" per ordinare il vettore degli 
//errori
    //e tener conto dell'indice corrispondente
    double temperr = 0;
    double tempind = 0;
    for (int i =0; i<nwords; i++){
            for (int j = 0; j<nwords; j++){
            //se ho superato i fallimenti concessi, IGNORO la 

//potenziale parola
            if (accettabile[i]==1){
                 if(err[j] < err[i]){

                       temperr = err[i];
                       err[i] = err[j];
                       err[j] = temperr;
                       best[i]=j;

                 };
            };
        };
    };
};






int main() {
    // your code goes here
    int nwords=3;
    int size = 5;
    double spettro[size];
    double datab [nwords] [size];
    double datasig [nwords] [size];
    double err[nwords] = {0};
    int accettabile[nwords];
    for (int i = 0; i<nwords; i++){accettabile[i] = 1;};
    int best[nwords] = {0};

    for (int i = 0; i<nwords; i++){
        for (int j = 0; j<size;j++){
        datab[i][j] = j + rnd();
        datasig[i][j] = (rnd() + rnd())/3.;

        };

    };  
    for (int i = 0; i<size;i++){spettro[i] = i+(2*rand() -1);};
    double *tmpdb = (double*)datab;
    double *tmpsig = (double*)datasig;
    confronto(spettro,&tmpdb,&tmpsig,err,accettabile,nwords,size,best);
    //  cout<< "migliori risultati e errori corrispondenti" << endl;
    //for (int i = 0; i<nwords; i++){
    //  if(best[i]!=0){
    //  cout << best[i] << "   " << err[i] << endl;};
    //};



return 0;

}

每当我 运行 代码(在 Ideone.com 上,由于我的 PC 的一些问题,我无法在任何本地 IDE 上编译),我得到提到的错误.我尝试 google 它但没有找到解决我的问题的方法。

我知道代码在性能方面可能不是最理想的,但整个项目并不那么繁重,所以我不寻求优化。

将二维数组传递给函数时不起作用。我在以下地方对您的代码进行了更改。

首先,将函数的定义修改为

void confronto(double*, double*, double*, double*, int*, int, int, int*);

以旧方式调用函数

double *tmpdb = (double*)datab;
double *tmpsig = (double*)datasig;
confronto(spettro, tmpdb, tmpsig, err, accettabile, nwords, size, best);

这样,二维数组被展开为一维数组并传递给函数,因此在函数中,您应该按以下方式使用数组:

err[i] += abs(spettro[j] - datab[i*size+j]);
if ((abs(spettro[j] - datab[i*size+j]) / datasig[i*size+j])>N){

应用上述更改后,它在我的计算机上运行良好。