According to the MCQ solution PDF answer to Q-4 is B that is Memory leak ,but memory leak happens when programmer forgets to free the memory . means if the programmer forgets to include the line free(ptr) then it is called as Memory leak .but in Q-4 code there is free(ptr) so it can’t be memory leak and it can’t be Dangling pointer because after free(ptr) we are not giving this pointer a new value to hold . Please clear this doubt by explaining the right answer.
Doubt in Q-4 about memory leak
@yashsaxena986 hey yash
1)A pointer pointing to a memory location that has been deleted (or freed) is called dangling pointer.
2)Memory leak occurs when programmers create a memory in heap and forget to delete it.
3)NULL Pointer is a pointer which is pointing to nothing. In case, if we don’t have address to be assigned to a pointer, then we can simply use NULL.
4) compiler error is i guess self explanatory.
answer explanation
it is so because first the memory is allocated to pointer using malloc(1st statement)
then the pointer is made to point to NULL. after that free statement is used which frees up the memory of ptr=NULL not the one allocated before. thus it leads to memory leak