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

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

本文关键字:3452 signal memory time Runtime error C++      更新时间:2023-10-16

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

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

#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上编译),我都会得到提到的错误。我试着谷歌一下,但没有找到任何解决我问题的方法。

我知道代码在性能方面可能是次优的,但整个项目不是那么重,所以我不寻找优化

当你将2d数组传递给函数时,它不起作用。我在以下地方修改了你的代码。

首先,将函数的定义更改为
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);

通过这种方式,2d数组被展开为1d数组并传递给函数,因此在函数中,您应该按照以下方式使用数组:

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

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