Pointers mcq question

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
i understand why B is correct but i am confused with C since it appears to be correct also

Hello @tusharaggarwal20132013,

  1. A memory leak occurs when programmers create a memory in heap and forget to delete it.
    In this program, after executing the following code the compiler will lose access to the memory assigned at run time using malloc()
    So, this leads to a memory leak.

  2. Computers crash because of errors in the operating system (OS) software or errors in the computer hardware.
    Reasons:
    2.1. Segmentation Fault: It is the major reason for the program to crash. These are maybe the reasons for such cause:

  • Attempting to access a memory location that doesn’t exist in our system.
  • There may be an attempt to write on a read-only memory location.
  • There may be an attempt to access protected memory location such as kernel memory
  • Stack Overflow: There may case of non-terminating recursion with the memory location.
    2.2. Buffer Overflow: It is an anomaly where a program while writing data to a buffer, overruns the buffer’s boundary and overwrites adjacent memory locations.
    2.3. Exceptions
  • Divide by zero.

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

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.