1
I want to do a recursive function to offset the memory of each block in the list, but when I print it after using the function, it loops and memory addresses are printed when asked to print (the correct would be to print nothing).
Statement in the main: in the *list;
Call on main: finaliza_recursivo(&list);
in the *list is only a pointer to the first element (block) of the list.
Below two versions of the same function, the two are giving the same problem:
Main
int main()
{
no *ini; //é um ponteiro para um bloquinho (inicio da lista)
elem x;
int erro;
cria(&ini);
x = 2;
inserir(&ini, &x, &erro);
x = 3;
inserir(&ini, &x, &erro);
x = 8;
inserir(&ini, &x, &erro);
imprimir(ini);
finaliza_recursivo(&ini);
return 0;
}
Function
void cria(no **inicio)
{
*inicio = NULL;
}
void finaliza_recursivo(no **inicio)
{
no *P;
P = *inicio;
if(P != NULL)
finaliza_recursivo(&P->prox);
free(P);
}
void finaliza_recursivo(no **inicio)
{
if(*inicio != NULL)
finaliza_recursivo(&(*inicio)->prox);
free(*inicio);
}
Just to clarify? Are you printing after calling this function? In general it is recommended to use a Minimum and verifiable example
– Mansueli
Your list is a correct chained list? If you are allocating the nodes using as pointer. Why are you using pointer pointer? If you did
no *n = (no*) malloc(sizeof(no))
just callfree(n)
.– Wakim
I was just printing to see if I had the memory displaced. I have another non-recurring version that is working and after calling it and then printing, do not print anything. When I use recursive, when printing, it prints random addresses. @Kyllopardiun
– Giovani
What I pass there at the beginning is just a pointer to the first node of the list. I want to make him go through the list and move every node out when he gets to the stop condition. @Wakim
– Giovani