Thread: segmentation fault

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    1

    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...

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > 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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    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
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault problem
    By odedbobi in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2008, 03:36 AM
  2. Segmentation fault
    By bennyandthejets in forum C++ Programming
    Replies: 7
    Last Post: 09-07-2005, 05:04 PM
  3. Segmentation fault
    By NoUse in forum C Programming
    Replies: 4
    Last Post: 03-26-2005, 03:29 PM
  4. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 01:33 PM
  5. Segmentation fault...
    By alvifarooq in forum C++ Programming
    Replies: 14
    Last Post: 09-26-2004, 12:53 PM