Why is answer of Q4 a memory leak?

If we free the heap memory how it is a memory leak?

Hello @Deepanshu_garg,

The problem is because of the statements:
ptr=NULL; //This is a NULL pointer
free(ptr);

The above code would crash on free(NULL), which is why you may see some defensive programming techniques recommend:

if (ptr != NULL)
free(ptr);

Syntax:
void free(void *ptr);
Description:
The free function causes the space pointed to by ptr to be deallocated, that is, made available for further allocation. If ptr is a null pointer, no action occurs.

Let’s understand it in detail:
Suppose, ptr is pointing to some memory location, lets say 0x100.

When you free(ptr), basically you are allowing 0x100 to be used by memory manager to be used for other activity or process and in simple words it is deallocation of resources.

When you do ptr=NULL, you are making ptr point to new location(lets not worry about what NULL is). Doing this you lost track of the 0x100 memory data.This is what is memory leak.

Hope, this would help.
Give a like, if you are satisfied.