I have such structure:
struct node *next;
struct node *link = (struct node*) malloc(sizeof(struct node));
link->key = 10;
link->data = 100;
link->next = link;
printf("%zd %zd %zd %zd\n", sizeof(link->key), sizeof(link->data), sizeof(link->next), sizeof(link));
4 4 8 8
printf("Size of link is %zd\n", sizeof(link));
printf("Now size of link is %zd\n", sizeof(link));
Size of link is 8
Now size of link is 8
So, the first question is why
link is a pointer. If you print
sizeof(*link), you would get a different number.
Why didn't freeing of memory happen?
Freeing memory does happen. It's just that it cannot be measured the way that you try it, i.e. with
sizeof. The size remains the same regardless of whether the pointer points to something valid or to something invalid.