C++ 'Runtime error time: 0 memory: 3452 signal:11'
C++ 'Runtime error time: 0 memory: 3452 signal:11'
我是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){
应用以上更改后,它在我的计算机上运行良好
相关文章:
- 调用 raise(signal) 会终止 c++ 程序
- 未调用 signal() 指定的处理程序
- C++中链表的错误"Abort signal from abort(3) (sigabrt) "
- 将成员函数传递给 std::signal
- 在 C++11 中,signal.h 标头没有 kill 函数
- QTableWidget dataChanged SIGNAL
- connect(QObject*, SIGNAL(signal()), functor) 在 qt5 中未连接
- "Static polymorphism with Qt signal/slot: What is wrong?"
- 为什么此程序以"unknown signal"终止?
- llvm 异常 - 提高异常 "?:Unknown signal"
- 为什么我可以通过自定义对话框获得"QMetaObject::connectSlotsByName: No matching signal"?
- Signal ReadyRead()不执行插槽
- Qt UDPsocket readyread() SIGNAL 发出两次
- QObject::connect not connecting signal to slot
- 对qt中的signal和slots使用另一个类对象
- QAbstractSpinBox - indexChanged signal
- SIGSEGV(signal 11) 'segmentation fault' in FCTRL2 codechef
- 如何将变量从main传递到signal和slot宏
- Android, CMDTool, stdout redirect, "Fatal signal 11 (SIGSEGV)"
- C++ 'Runtime error time: 0 memory: 3452 signal:11'