Mcq pointers doubt

Q­4 What is the problem with the following code #include<stdio.h>
int main()
{
int *ptr = (int *)malloc(sizeof(int));
ptr = NULL;
free(ptr);
}
A) Dangling Pointer
B) Memory leak
C) The program may crash as free() is called for NULL pointer.
D) Compiler Error

why is option B memory leak the answer.I cant understand what memory leak happens here?

hello @kani001

malloc return base address of allocated memory.
which we are storing in ptr. so the only way of accessing the allocated memory is that pointer.

here we are changing the address of pointer to NULL. that means now we dont have that base adress of allocated memory due to which we cant acess or use that memory(we dont know its address) .
that why we call it memory leak i.e (allocated memory is there but we cant access it because we lost its address)

what free(ptr) does here and even though we free the pointer does the memory allocated whose address we lost cant be deallocated?

free is used to deallocate memory whose address is contained in ptr.
but ptr is containing NULL.
thats why it will not release our actual allocated memory

so,that allocated memory basically becomes waste right?until some ptr is made to point to the same address…and so we say there is a memory leak…

yeah correct . . . . . . . .