Memory Allocation - Infinite loop when using the free() function

Asked

Viewed 96 times

0

I’m doing a code in C to test concepts of data structure that are passed through my university. But I came across an error when using the function free, that generates an infinite loop in my code. If I remove it the code works. I used the free in function InserePrimeiro (Gentlemen, I am not responsible for the names of the functions). If anyone can guide me the solution I would appreciate.

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

1 answer

2

You just did the malloc of the element novo, soon makes no sense to effect the (free) release of it just below. If you do, the memory will be out of place as soon as the method returns. When trying to print the value after entering, the code will attempt to access an unallocated region (the first element, which had memory allocated to it, was released soon after), which results in undefined behaviour, which basically means anything can happen, then your program freezes.

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

  • in Retira(), which is where the free() should happen, he’s allocating one more element, for reasons I can’t discern.

  • Yes, this memory is leaking, nor had noticed... by the way the comrade is beginning to learn. I addressed in the reply only the initial petition :)

  • Yes, gentlemen, I’m not familiar with C and those concepts. I am a student of computer science and this subject is data structure I. I will strive to understand everything well. Thanks for the clarifications. D

Browser other questions tagged

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