Open In App
Related Articles

C | Dynamic Memory Allocation | Question 7

Improve Article
Save Article
Like Article

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

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 28 Jun, 2021
Like Article
Save Article
Similar Reads