Top

Discussion

What is the output of this program?

#include 
void main()
{
   int *ptr = (int *)malloc(sizeof(int));
   *ptr = 10;
   free(ptr);
   p = 5;
   printf("%d", ptr);
}

 

  • A.Compilation error
  • B.5
  • C.Garbage value

Answer: B

 

free() will not deallocate the memory it just to delete all data's allocated to a variable (ptr). In this program, first integer value 10 is assigned to the pointer variable *ptr and then its data is deleted using free(ptr) and then new integer value 5 is assigned to the variable ptr and then 5 is outputted.

No comment is present. Be the first to comment.
Loading…

Post your comment