I have the following C code:
#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
int a;
}node;
int main()
{
node * n;
printf("\n%d\n",n->a);
n = (node *) malloc ( sizeof( node ));
printf("\n%d\n",n->a);
n->a = 6;
printf("\n%d\n",n->a);
free(n);
printf("\n%d\n",n->a);
n->a = 4;
printf("\n%d\n",n->a);
return 0;
}
The result obtained:
1314172
0
6
0
4
My question is even after free (n), why n-> a = 0 and how can we reassign it to any value, such as n-> a = 4
Is the block of memory pointed to by n invalid?
S..K source
share