-
segmentation fault
I was trying to convert my Linked List into a Circular Linked List (everything worked ok with the not circular one)... but then when I do the insert (inserta) operation, and test it... DOS prompt closes itself (crashes)... then when I debug it, it says I have an acces violation (segmentation fault)...
So... why?! Code:
Code:
#include <stdio.h>
#include <stdlib.h>
typedef int tipoDato;
typedef struct tipoNodo{
tipoDato elem;
struct tipoNodo *sig;
}tipoNodo;
typedef tipoNodo *tipoLista;
typedef tipoNodo *tipoPos;
void inicializa (tipoLista *r){
*r=NULL;
}
int vacia (tipoLista *r){
return!*r;
}
tipoPos primero (tipoLista *r){
return *r;
}
tipoPos ultimo (tipoLista *r){
tipoPos aux;
if (vacia(r))
return NULL;
aux=*r;
while(aux->sig!=*r)
aux=aux->sig;
return aux;
}
tipoPos anterior (tipoPos pos, tipoLista *r){
tipoPos aux;
if(vacia(r)||pos==NULL)
return NULL;
aux=*r;
while(aux->sig!=pos && aux!=NULL)
aux=aux->sig;
return aux;
}
tipoPos siguiente(tipoPos pos, tipoLista *r){
if(vacia(r)||pos==NULL)
return NULL;
return pos->sig;
}
void inserta (tipoDato e, tipoPos pos, tipoLista *r){
tipoPos aux;
aux=(tipoPos)malloc(sizeof(tipoNodo));
if(aux==NULL){
printf("memoria del sistema insuficiente");
return;
}
aux->elem=e;
if (pos==NULL){
aux->sig=*r;
ultimo(r)->sig=aux;
*r=aux;
}
else{
aux->sig=pos->sig;
pos->sig=aux;
}
}
void elimina (tipoPos pos, tipoLista *r){
if(vacia(r)||pos==NULL){
printf("insuficiencia de datos");
return;
}
if (pos->sig=pos)
*r=NULL;
else if (pos==*r){
ultimo(r)->sig=(*r)->sig;
*r=(*r)->sig;
}
else
anterior(pos,r)->sig=pos->sig;
free(pos);
}
tipoPos localiza (tipoDato elem, tipoLista *r){
tipoPos aux;
aux=*r;
while (aux!=NULL&&aux->elem!=elem)
aux=aux->sig;
return aux;
}
tipoDato recupera (tipoPos pos, tipoLista *r){
if(vacia(r)||pos==NULL){
printf("insuficiencia de datos, el programa se terminara");
exit(1);
}
else
return pos->elem;
}
void imprime (tipoLista *r){
tipoPos aux;
aux=*r;
while (aux!=NULL){
printf("%d ",aux->elem);
aux=aux->sig;
}
}
void anula (tipoLista *r){
tipoPos aux;
while(*r!=NULL){
aux=*r;
*r=aux->sig;
free(aux);
}
}
int main(int argc, char *argv[]){
tipoLista r;
int i;
inicializa(&r);
if(vacia(&r))
printf("la lista esta vacia\n\n");
system("cls");
for(i=0;i<20;i++)
inserta(rand()%100,ultimo(&r),&r);
imprime(&r);
printf ("\n\n");
elimina(ultimo(&r),&r);
elimina(primero(&r),&r);
imprime(&r);
printf ("\n\n");
elimina(anterior(ultimo(&r),&r),&r);
elimina(siguiente(primero(&r),&r),&r);
imprime(&r);
printf ("\n\n");
for(i=0;i<50;i++){
if(localiza(i,&r)!=NULL)
elimina(localiza(i,&r),&r);
}
imprime(&r);
printf ("\n\n");
anula(&r);
if(vacia(&r))
printf("la lista esta vacia\n\n");
system("PAUSE");
return 0;
}
Thanks in advance...
-
> then when I debug it, it says I have an acces violation (segmentation fault)...
So start with that then.
For example, you should have a stack trace of where the problem occurred, say
ultimo
inserta
main
Look at the variables, do they make sense? They shouldn't at the segfault point.
Work backwards through the execution path to where it could have gone wrong, and set a breakpoint, then re-run the code.
When you hit the breakpoint, look at the variables and see what happens next.
-
Code:
void inserta (tipoDato e, tipoPos pos, tipoLista *r){
tipoPos aux;
aux=(tipoPos)malloc(sizeof(tipoNodo));
if(aux==NULL){
printf("memoria del sistema insuficiente");
return;
}
aux->elem=e;
if (pos==NULL){
aux->sig=*r;
ultimo(r)->sig=aux;
*r=aux;
}
else{
aux->sig=pos->sig;
pos->sig=aux;
}
}
on the first insert list is empty, ultimo(r) will return NULL, dereferencing NULL-pointer will cause the crash