Segmentation fault: Linked lists in C

Asked

Viewed 194 times

1

I’m playing a little C here and I found that in the second insertion I do through the console that the lista loses the first pointer(pointer) created in the first insertion creating a

Segmentation fault; dumped core;

Does anyone know why?

I have a structure like this:

typedef struct livro 
{
    char *nome;
    int numLivros;
    struct livro *next;        
} Livro;

Function of allocating a book:

Livro* alocaLivro( char *nome, int num) 
{
    Livro *novo     = (Livro *) malloc(sizeof(Livro));
    char *novo_nome = (char *)  malloc(sizeof(char) * MAX_NOME_LIVRO);
    strcpy(novo_nome, nome);

    novo->nome      = novo_nome;
    novo->numLivros = num;
    novo->next = NULL;

    return novo;
}

Insert book in list function:

int insertLivroCauda(Livro **lista, char *nome , int num) 
{
    Livro *novo = alocaLivro(nome, num);
    if (!novo)
        return 0;

    if(*lista == NULL)
    {
        (*lista) = novo;
    }
    else
    {
        Livro *aux = (*lista);
        while (aux->next != NULL) 
        {
            aux = aux->next;
        }
        aux->next = novo;
    }
    return 1;
}

Main:

int main(int argc, char** argv) 
{
    Livro *lista = NULL;
    char opt;
    char *my_string;

    printf("1 - Insirir novo livro\n");
    printf("2 - Remover último livro\n");

    while (scanf("%s",&opt))
    {   
        switch(opt)
        {
            case '1':
                printf("Insirir nome livro:\n");

                my_string = (char*) malloc (sizeof(char) * MAX_NOME_LIVRO );

                scanf("%s",my_string);

                insertLivroCauda(&lista, my_string, sizeof(my_string));

                break;

            default:
                return (EXIT_SUCCESS);    
        }
        printf("1 - Insirir novo livro\n");
        printf("2 - Remover último livro\n");
    }
    return (EXIT_SUCCESS);
}

2 answers

4


It may not be the cause of your problem, but it’s already a very serious problem:

You have:

char opt;
scanf("%s", &opt);

The scanf is reading a string of size boundless which will be put in the array opt. If it is an empty string, then it will be composed of an array with only the null terminator, in which case it works and opt=0. But if you go in with anything else, like "1", already need two char. The function scanf will blindly write memory beyond the variable. Then you get to the world of Undefined Behavior and there’s nothing more to analyze.

Correction:

scanf("%c", &opt);  // leia apenas um char
  • Actually the problem was in reading and not pointing us. I created a function for reading that solved the problem.

0

As I did not understand very well what you were trying to do I tried to follow a logic that in the role of inserting book you needed to pass the List, the name of the book, and the size of it... so I gave a scramble in the main Might help with something in the code. your book insert function is receiving as parameter a pointer to Book pointer, and you’re passing it Null, well I didn’t understand it right and I didn’t move.

int main() 
{
    //Livro *lista = NULL; /// ???
    int opcao;
    char *CleanBuffer;
    char *Nomelivro;
    char tmdLivro;

    do   
    {   
        printf("1 - Insirir novo livro\n");
        printf("2 - Remover último livro\n");
        printf("3 - Encerrar Aplicacao \n");

        scanf("%d", &opcao);
        scanf("%c", &CleanBuffer); 

        switch(opcao)     
        {
            case 1:
                printf("Insira o nome do livro: ");
                gets(Nomelivro);
                tmdLivro = strlen(Nomelivro);          
                insertLivroCauda(//????, &Nomelivro, tmdLivro);

                break;
               ///End Case.

            default:
                return 0;   
        }
    }while (opcao != 3);

    return 0;
}
  • I start with a pointer lista null and step the reference of that pointer. Then in the insert book I check if it is null the new book to insert becomes the pointer lista ((*lista) = novo;)

  • Buddy, would it be cool if you set a macro for this list with a no value? it would already enter a value defined by you at the time of the check, and just compare.

  • The problem was not in the insert function, it is in the reading that was not working. I just solved the problem, instead of using gets I’m using a character reading function of my own.

  • Great! who good then that solved :P

  • But the do while and the CleanBuffer greatly help in this matter.

  • Ah and the gets is obsolete, is no longer used.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.