c++ - Segmentation fault on return -
i segmentation fault on return of function (inside last for), , have no idea why (i'm going crazy, because can't make sense out of problem).
frase global variable. function copy of function works , modified last (i write comment before show better). if function returns @ line {cout<<"404 "<<endl;return 404;} no error. hope have explained that's necessary.
int funzione_principale() { //string frase; char frase_char [1024]={'\0'}; char frase_elaborata [1024]={'\0'}; playsound(text("avviso.wav"), null, snd_filename); rec(); //registro l' audio while(active) sleep(50); // here wait other function complete! if(frase.length()<=14) { cout<<"404 "<<endl;return 404; } frase.copy(frase_char,frase.length()-57,56); for(int k=0; frase_char[k]!='"';k++) { frase_elaborata[k]=frase_char[k]; } //tramite fparole otteniamo il numero di parole contenute nella frase_elaborata righe=fparole(frase_elaborata); //manipolazione delle parole nelal frase tramite matrice char matrice [righe][27]; for(int x=0;x<=righe;x++) memset(matrice[x],0,27); int conto=0; int indice=0; int riga=0; int lunghezza=strlen(frase_elaborata); minuscole_accenti(frase_elaborata); cout<<"frase tradotta: "<<frase_elaborata<<endl; while (frase_elaborata[conto]!='\0') { if(frase_elaborata[conto]>=97 && frase_elaborata[conto]<=122) { matrice[riga][indice]=frase_elaborata[conto]; indice++; } if(frase_elaborata[conto]>=48 && frase_elaborata[conto]<=57) { matrice[riga][indice]=frase_elaborata[conto]; indice++; } if(frase_elaborata[conto]==' ' && conto!=lunghezza-1 && frase_elaborata[conto+1]!=' ') { matrice[riga][indice]='\0'; indice=0; riga++; } conto++; } //adesso la matrice contiene num righe con ogniuna, una parola della frase cout<<"frase presente nella matrice: "<<endl; for(int x=0;x<=righe;x++) cout<<matrice[x]<<endl; // here modified function ! for(int t=0;t<=righe;t++) { if(strcmp(matrice[t],"s")==0) {return 1;} if(strcmp(matrice[t],"si")==0) {return 1;} if(strcmp(matrice[t],"affermativo")==0) {return 1;} if(strcmp(matrice[t],"ok")==0) {return 1;} if(strcmp(matrice[t],"certo")==0) {return 1;} if(strcmp(matrice[t],"no")==0) {return -1;} if(strcmp(matrice[t],"negativo")==0) {return -1;} if(strcmp(matrice[t],"dopo")==0) {return 0;} if(strcmp(matrice[t],"rimanda")==0) {return 0;} if(strcmp(matrice[t],"rimandalo")==0) {return 0;} if(strcmp(matrice[t],"non")==0 && strcmp(matrice[t+1],"ora")==0) {return 0;} if(strcmp(matrice[t],"chiedi")==0 && strcmp(matrice[t+1],"dopo")==0) {return 0;} if(strcmp(matrice[t],"chiedimelo")==0 && strcmp(matrice[t+1],"dopo")==0) {return 0;} } return 404; }
you have problem in 2 places (same problem). for loop runs 0 righe inclusive, , twice. 1 time write illegal memory space (for(int x=0;x<=righe;x++) memset(matrice[x],0,27);), , 2nd time read it. should for(int x=0;x<righe;x++).
Comments
Post a Comment