Delete queue element and release memory for this function?

Asked

Viewed 36 times

0

I’m starting to understand pointers better and I’m implementing a queue, and it follows the function:

FILA *removeNodeFIFO(FILA **raiz)
{
    FILA *aux = *raiz;
    if (aux == NULL)
    {
        return NULL;
    }
    else
    {
        if (aux->prox == NULL)
        {

            *raiz = NULL;
            return aux;
        }
        else
        {
            *raiz = aux->prox;
            return aux;
        }
    }
}

I’m going through the root by reference, since I have to update the root and return the deleted element, but I don’t know how I would return the deleted element and then move it out of memory, so I would need to use free(aux) right after Return, how can I release after returned?

By doing so, am I releasing her? supposing I’m calling her in another capacity:

FILA *cel = removeNodeFIFO(&raiz);
...Faço o que preciso com o nó retornado...
free(cel);

1 answer

0


Yes, it’s releasing! You can check the addresses with printf("%p");

Puts a:

printf("%p\n", aux);

before returning it, and a:

printf("%p\n", cel);

before giving free, for you to see that it is the same address.

Browser other questions tagged

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