What problem may occur with the below code?

164. What problem may occur with the below code?

#include <stdio.h>
int main()
{
    float* ptr = (int*)malloc(sizeof(float));
    ptr = NULL;
    free(ptr);
}

Options:

  1. Memory leak
  2. Dangling pointer
  3. Compiler error
  4. None of the above

Answer: A) Memory leak

Explanation:

The above program will create a memory leak because we assigned the NULL to the pointer and free the pointer.

Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.