Skip to content
Related Articles

Related Articles

Improve Article
C | Dynamic Memory Allocation | Question 7
  • Difficulty Level : Basic
  • Last Updated : 03 Sep, 2013

What is the problem with following code?

int main()
    int *p = (int *)malloc(sizeof(int));
    p = NULL;

(A) Compiler Error: free can’t be applied on NULL pointer
(B) Memory Leak
(C) Dangling Pointer
(D) The program may crash as free() is called for NULL pointer.

Answer: (B)

Explanation: free() can be called for NULL pointer, so no problem with free function call.

The problem is memory leak, p is allocated some memory which is not freed, but the pointer is assigned as NULL. The correct sequence should be following:

    p = NULL;

Quiz of this Question

Want to learn from the best curated videos and practice problems, check out the C Foundation Course for Basic to Advanced C.
My Personal Notes arrow_drop_up
Recommended Articles
Page :