Memory allocation - Infinite loop when using free () function

0

I am making a C code to test Data Structure concepts that are passed in my university. However I came across an error when using the free function, which generates an infinite loop in my code. If I remove it, the code works. I used free in the InserePrimeiro function (Sirs, I'm not responsible for the names of the functions). If anyone can guide me through the solution I would appreciate it.

#include<stdio.h>
#include<stdlib.h>

typedef struct Nodo {
    int valor;
    struct Nodo *proximo;
} Nodo;

typedef struct Lista {
    Nodo *primeiro;
    Nodo *ultimo;
} Lista;

void FLVazia(Lista *l) {
    l->primeiro = NULL;
    l->ultimo = NULL;
}

int Vazia(Lista l) {
    if (l.primeiro == NULL)
        return 1;
    else return 0;
}

void InserePrimeiro(int x, Lista *l) {
    Nodo *novo = (Nodo *) malloc(sizeof(Nodo));
    novo->valor = x;
    novo->proximo = NULL;
    l->primeiro = novo;
    l->ultimo = novo;
    free(novo);
}

void Insere(int x, Lista *l) {
    Nodo *novo = (Nodo *) malloc(sizeof(Nodo));
    novo->valor = x;
    novo->proximo = NULL;
    l->ultimo->proximo = novo;
    l->ultimo = novo;
}

void Imprime(Lista *l) {
    Nodo *aux = (Nodo *) malloc(sizeof(Nodo));
    aux = l->primeiro;
    while(aux != NULL) {
        printf("%d\n", aux->valor);
        aux = aux->proximo;
    }
}

int Acessa(int p, Lista *l) {
    Nodo *aux = (Nodo *) malloc(sizeof(Nodo));
    aux = l->primeiro;
    int i;
    for(i=1; i<p; i++) {
        aux = aux->proximo;
    }
    return aux->valor;
}

int Retira(int p, Lista *l) {
    Nodo *aux = (Nodo *) malloc(sizeof(Nodo));
    aux = l->primeiro;
    int removido;
    int i;
    for(i=1; i<p-1; i++) {
        aux = aux->proximo;
    }
    removido = aux->proximo->valor;
    aux->proximo = aux->proximo->proximo;
    return removido;
}

void main() {
    Lista li;
    FLVazia(&li);
    int valor;
    int option=100;
    while(option != 0 ) {
        printf("Escolha uma opcao:\n");
        printf("1 - Verifica se a lista esta vazia\n");
        printf("2 - Inserir na primeira posicao\n");
        printf("3 - Insere um item na ultima posição\n");
        printf("4 - Acessa uma posicao\n");
        printf("5 - Retira um item da lista\n");
        printf("6 - Imprime a lista\n");
        printf("0 - Sair\n");
        scanf("%d",&option);
        switch(option) {
        case 0:
            printf("Saindo...");
            break;
        case 1:
            printf("Lista vazia?:%d\n",Vazia(li));
            break;
        case 2:
            printf("Valor:");
            scanf("%d",&valor);
            InserePrimeiro(valor,&li);
            Imprime(&li);
            break;
        case 3:
            printf("Valor:");
            scanf("%d",&valor);
            Insere(valor,&li);
            break;
        case 4:
            printf("posicao:");
            scanf("%d",&valor);
            printf("%d\n",Acessa(valor,&li));
            break;
        case 5:
            printf("posicao:");
            scanf("%d",&valor);
            printf("removido: %d\n",Retira(valor,&li));
            break;
        case 6:
            Imprime(&li);
            break;
        }
    }
}
    
asked by anonymous 16.06.2017 / 14:29

1 answer

2

You have just done malloc of element novo , so it does not make sense to do the freeing of it just below. If you do, the memory will be deallocated as soon as the method returns. When trying to print the value after insertion, the code will try to access an unallocated region (the first element, which had memory allocated to it, was released soon after), resulting in undefined behavior , which basically means that anything can happen, hence your program freezes.

Then the answer is already in your question: just remove the free method InserePrimeiro and your code will be ok.

    
16.06.2017 / 14:53