2
When implementing a chained list, I found the following problem in the function that inserts nodes at the end of the list:
Program Received Signal SIGSEGV, Segmentation fault. 0x000000400d2f in insere_fim (ref=0x0, value=1) at main.cpp:82
82 if(ref == nullptr) ref->Prox = newNo;Program terminated with Signal SIGSEGV, Segmentation fault.
The program no longer exists.
If I insert the third node of the list through the end inserts the program works, but if I call the end inserts before that, the above error occurs. The line that presents the error is this:
if(ref == nullptr) ref->prox = novoNo;
I have tried redoing the function and using other compilers, but the error persists. Below is the code of the structure and functions inserts and inserts end and menu function, responsible for calling the other functions:
struct lista {
int info;
lista* prox;
};
lista* criaLista() {
cout << "Lista criada." << endl;
return nullptr;
}
lista* insere(lista* ref, int valor) {
lista* novoNo = nullptr;
novoNo = new lista;
if(!novoNo){
printf("Sem memoria disponivel!\n");
return ref;
}
novoNo->info = valor;
novoNo->prox = ref;
ref = novoNo;
cout << "Nodo com valor " << novoNo->info << " inserido." << endl;
return ref;
}
void insere_fim(lista* ref, int valor)
{
lista* novoNo=nullptr;
novoNo= new lista;
if(!novoNo){
printf("Sem memoria disponivel!\n");
return;
}
novoNo->info=valor;
novoNo->prox = nullptr;
if(ref == nullptr) ref->prox = novoNo;
else{
lista* p = ref->prox;
while(p->prox != nullptr)
p = p->prox;
p->prox = novoNo;
}
cout << "Nodo com valor " << novoNo->info << " inserido." << endl;
}
void menu(int* op, lista* ref) {
int valor, aux=1;
cout << "\033[2J\033[1;1H";
cout << " --- Lista encadeada ---\n" << endl;
while(true) {
cout << "\n 1- Criar lista 5- Inserir no fim da lista\n 2- Inserir nodo 6- Liberar memoria\n 3- Imprimir lista 7- Encerrar programa\n 4- Remover nodo\n\nSelecione uma acao: ";
cin >> *op;
if((aux==1) && ((*op!=1) && (*op!=7))) {
cout << "ERRO! Inicie o programa criando uma lista.\n";
continue;
}
switch (*op) {
case 1 : ref=criaLista(); aux=0;break;
case 2 : {
cout << "Valor do nodo: ";
cin >> valor;
ref=insere(ref, valor); break;
}
case 3 : imprime(ref); break;
case 4 : {
cout << "Valor do nodo: ";
cin >> valor;
ref=remove(ref, valor); break;
}
case 5 : {
cout << "Valor do nodo: ";
cin >> valor;
insere_fim(ref, valor); break;
}
case 6 : libera(&ref); break;
case 7 : break;
default : cout << "Acao invalida! Tente novamente." << endl;
}
if (*op==7) {
cout << "\n Fim do programa! :D ";
break;
}
}
}
int main () {
int op; lista* ref;
menu(&op, ref);
}
Nereu, would this help you? (in English) Segmentation fault in Linked list implementation in C++
– Luiz Augusto
I don’t think so, Luiz. The problem with this link is in while, while my problem is in if.
– Nereu Melo
Can you provide the code you are using to enter the nodes ? That is, what is in the
main
.– Isac
Yes. I just updated the post.
– Nereu Melo
Confirm the edit you made. Your
insere
seems to have been halfway through, no code or}
after theif
checking if memory is available.– Isac
Sorry, I’ve corrected the function.
– Nereu Melo
Also include the function
criaLista
that this is relevant to the problem.– Isac
Okay, I just entered it. I chose not to show you the whole code to avoid visual pollution. If you want, I can put it full.
– Nereu Melo
It is not necessary, but the question has to have the relevant functions to the problem, in case it lacked to create, because depending on how it creates the error or confusion can be another. The ideal question is to have the bare minimum.
– Isac