MSQ
Consider the following C snippet:
#include <stdio.h>
int main()
{
int *ptr = (int*) malloc(100*sizeof(int));
*ptr=33;
printf("%d %d\n",ptr,*ptr); // Line X
free(ptr);
*ptr=37;
printf("%d %d\n",ptr,*ptr); // Line Y
return 0;
}
Which of the following is TRUE regarding “Line X” and “Line Y”
- Printed value of ptr (heap address) is same for both “Line X” and “Line Y”
- Printed value of ptr (heap address) is not same for both “Line X” and “Line Y”, as in Line Y, ptr is a dangling pointer and will print garbage value
- The value of *ptr (value at *ptr) is 37 in Line ‘Y’
- The value of ptr (value at *ptr) is 0 as in Line Y, the allocated heap is freed, and we cannot update it anymore, as ptr is now a dangling pointer