Segmentation fault: Lists linked in C

1

I'm here to play a little bit with C and I found that in the second insert I make through the console that lista loses the first pointer created in the first insertion creating a

  

Segmentation fault; core dumped;

Does anyone know why?

I have a structure like this:

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

Book Allocation Function:

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;
}

Book insert function in the list:

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);
}
    
asked by anonymous 04.12.2014 / 19:18

2 answers

4

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

You have:

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

The scanf is reading an unlimited string string that will be placed 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 enter with anything else, like "1" , you already need two char . The scanf function will blindly write memory beyond the variable. Then there is the world of Undefined Behavior and there is no more to analyze.

Correction:

scanf("%c", &opt);  // leia apenas um char
    
04.12.2014 / 19:39
0

As I did not quite understand what you were trying to do I tried to follow a logic that in the function of inserting a book you had to go through the List, the name of the book, and the size of it ... so I gave a main might help with something in the code. its function of inserting book is receiving as a parameter a pointer to Book pointer, and you are passing it Null, well I did not understand it correctly and did not 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;
}
    
05.12.2014 / 17:59