Thread: Segmentation fault in linked list...doubt

  1. #1
    Registered User
    Join Date
    Jul 2012
    Posts
    53

    Segmentation fault in linked list...doubt

    Hi

    I have seg fault in this code, any help will be apreciated.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    typedef struct churr{
    
    
        char mat[100];
        char pedido[100];
        float preco;
    }INFO;
    
    
    typedef struct elem{
        INFO nodo;
        struct elem *Seguinte;
        struct elem *Anterior;
    }ELEM;
    
    
    int recebeCliente(INFO info, ELEM **initlista, ELEM **fimlista)
    {
        ELEM *novo;
        novo=(ELEM*)malloc(sizeof(ELEM));
        novo->nodo=info;
        novo->Seguinte=NULL;
        novo->Anterior=NULL;
        if(*initlista==NULL)
        {
            novo=*initlista;
            novo=*fimlista;
        }
        novo->Anterior=*fimlista;
        (*fimlista)->Seguinte=novo;
        *fimlista=novo;
    return 0;
    }
    int main()
    {
        ELEM *initlista=NULL, *fimlista=NULL;
        INFO info;
        printf("Matricula: \n");
        gets(info.mat);
        printf("Descricao: \n");
        gets(info.pedido);
        printf("Preco: \n");
        scanf("%f",&info.preco);
        getchar();
        recebeCliente(info,&initlista,&fimlista);
        return 0;
    }
    In this line
    novo->Anterior=*fimlista;

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    If your code was in English it would be much more easier to help you.However you may say which is this language,so we can translate anything if necessary

  3. #3
    Registered User
    Join Date
    Jul 2012
    Posts
    53
    The language is portuguese.
    But most of the code is english, some words in Portuguese.
    But if you have any doubt about any word just ask.

    Many thanks

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Code:
        if(*initlista==NULL) 
        {
            novo=*initlista; /* You assign the value of NULL to novo */
            novo=*fimlista; /* Next you assigned an unknown value likely NULL again to novo */
        }
        novo->Anterior=*fimlista; /* Now you deference a NULL pointer */
    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  5. #5
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Code:
    novo=*fimlista; /* Next you assigned an unknown value likely NULL again to novo */
    Sure it is null.Check line 40 in the first post.So was that the problem Tim pointed out?

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Could it be that novo is NULL. Possibly because of the following snippet:
    Code:
        if(*initlista==NULL)
        {
            novo=*initlista;
            novo=*fimlista;
        }
    When I ran this through my debugger it executed this if and at the end novo was NULL.

    Jim

  7. #7
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    As i stated above novo is certaintly NULL,because at line 40 he declares *initlista and *fimlista and sets them at NULL.Then he does not do anything with them and at line 49 he calls a function,so both the pointers are set to NULL and so novo is NULL.

  8. #8
    Registered User
    Join Date
    Jul 2012
    Posts
    53
    So what i see is it is a bad implementation, nothing will be writed in that list?
    I just try tu use a print function and nothing is printed

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    typedef struct churr{
    
    
        char mat[100];
        char pedido[100];
        float preco;
    }INFO;
    
    
    typedef struct elem{
        INFO nodo;
        struct elem *Seguinte;
        struct elem *Anterior;
    }ELEM;
    
    
    int recebeCliente(INFO info, ELEM **initlista, ELEM **fimlista)
    {
        ELEM *novo;
        novo=(ELEM*)malloc(sizeof(ELEM));
        novo->nodo=info;
        novo->Seguinte=NULL;
        novo->Anterior=NULL;
        if(novo==NULL)
        {
            printf("Erro na lista...");
            return -1;
        }
        if(*initlista==NULL)
        {
            novo=*initlista;
            novo=*fimlista;
        }
        else
        {
            novo->Anterior=*fimlista;
            (*fimlista)->Seguinte=novo;
            *fimlista=novo;
        }
    return 0;
    }
    
    
    int listar(ELEM *initlista)
    {
        ELEM *aux;
        aux=initlista;
        while(aux!=NULL)
        {
            printf("%s %s %.2f", aux->nodo.mat, aux->nodo.pedido, aux->nodo.preco);
            aux=aux->Seguinte;
        }
        return 0;
    }
    int main()
    {
        ELEM *initlista=NULL, *fimlista=NULL;
        INFO info;
        printf("Matricula: \n");
        gets(info.mat);
        printf("Descricao: \n");
        gets(info.pedido);
        printf("Preco: \n");
        scanf("%f",&info.preco);
        getchar();
        recebeCliente(info,&initlista,&fimlista);
        listar(initlista);
        return 0;
    }

    *No more warnings with
    {

    }
    in insert funtion
    Last edited by alphasil; 07-11-2012 at 04:49 PM.

  9. #9
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Yes,because it aux is NULL at line 52.
    Code:
    if(novo==NULL)
        {
            printf("Erro na lista...");
            return -1;
        }
    This is at wrong point.You should write it after the if else structure you use in function recebeCliente

  10. #10
    Registered User
    Join Date
    Jul 2012
    Posts
    53
    I have changed those lines after the if else structure and now i have

    "Erro na lista", Error in list

    So nothing is writed in that list, the strange is the pdf the teacher gave me has this...!!!

  11. #11
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Teacher is a human,so he or she is able to makes mistakes.What do you want to do?Shall we try to correct it?

  12. #12
    Registered User
    Join Date
    Jul 2012
    Posts
    53
    I want to insert clients into that list.

    Check the example he gave us...

    Code:
     
    void insereIni(int num){
     
    Lista* novo = (Lista*) malloc(sizeof(Lista)); 
    novo->conteudo = num;  
    novo->anterior= NULL;  
    novo->proximo= NULL;  
    if (lini==NULL) {    
    lini=novo;
    lfim=novo;
    }
     else 
    {
     
    novo->proximo = lini;  
    lini->anterior=novo;   
    lini=novo;

  13. #13
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Code:
    if (lini==NULL) {   
    lini=novo;  /* NOTE: novo value is being assigned to lini */
    lfim=novo; /* NOTE: novo value is being assigned to lfim */
    }
    Now read your code; which side of the "=" equals sign is novo on?

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  14. #14
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    What is Lista,lini,lfim?

  15. #15
    Registered User
    Join Date
    Jul 2012
    Posts
    53
    Lista is list

    lini is a variable to define initList, the other endList

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Doubly linked list-segmentation fault
    By prapanch in forum C Programming
    Replies: 2
    Last Post: 05-10-2012, 11:04 PM
  2. Doubly Linked List Segmentation Fault
    By DaNxTh3xMaNx in forum C Programming
    Replies: 5
    Last Post: 09-09-2011, 09:50 AM
  3. Segmentation Fault and linked lists
    By VitorTomaz in forum C Programming
    Replies: 2
    Last Post: 11-09-2010, 12:51 PM
  4. Segmentation Fault with Linked Lists
    By mkylman in forum C Programming
    Replies: 1
    Last Post: 04-27-2010, 11:30 PM
  5. Linked list Segmentation
    By daisy_polly in forum C Programming
    Replies: 9
    Last Post: 03-20-2006, 03:31 AM