Alan Alan - 2 months ago 24
C++ Question

Segmentation fault on return

I get a segmentation fault on return of this function (inside the last for), and I have no idea why (I'm going crazy, because can't make sense out of this problem).

frase
is a global variable. The function is a copy of another function that works and I modified only the last for (I write a comment before to show you better). If the function returns at line
{cout<<"404 "<<endl;return 404;}
I get no error. I hope I have explained everything 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 I WAIT OTHER FUNCTION TO 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;

// FROM HERE I MODIFIED THE 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;
}

Answer

You have a problem in 2 places (same problem). Your for loop runs from 0 to righe INCLUSIVE, and you do that twice. One time you write to illegal memory space (for(int x=0;x<=righe;x++) memset(matrice[x],0,27);), and the 2nd time you read it. The for should be for(int x=0;x<righe;x++).

Comments