I have this piece of code:
struct nodo {
char c;
int val;
struct nodo *pizq, *pder;
};
typedef struct nodo NODO;
With its proper initializer function:
NODO * crea_nodo_int (int valor )
{
NODO *p;
p = (NODO *)malloc( sizeof( NODO ) );
if (p == NULL )
return NULL;
p -> pizq = NULL;
p -> pder = NULL;
p -> val = valor;
return p;
}
On the main function I have:
NODO * test = crea_nodo_int(5);
free(test);
inorder(test);
As you can see it's an implementation of a binary tree where pizq and pder are left and right children respectively.
However I'm posting this because I had understood that 'free()' actually deallocates memory and I can't see this here. You see, I create node test with value of 5, then free it, then when I walk inorder in the console it prints
'0' and terminates. Why the hell is printing 0? Shouldn't it go straight to the case where if (p == NULL) return
and terminate without printing anything? What am I doing wrong?
Here's my code for the inorder function:
void inorder( NODO *p )
{
if (p == NULL) return;
if( p->pizq != NULL )
inorder(( p->pizq );
// Process root
printf("%d ", p->val );
if( p->pder != NULL )
inorder( p->pder );
}