Which of the following should be used for freeing the memory allocated in the following C code?
#include <stdio.h>
struct p
{
struct p *next;
int x;
};
int main()
{
struct p *p1 = (struct p*)malloc(sizeof(struct p));
p1->x = 1;
p1->next = (struct p*)malloc(sizeof(struct p));
return 0;
}
#include <stdio.h>
struct p
{
struct p *next;
int x;
};
int main()
{
struct p *p1 = (struct p*)malloc(sizeof(struct p));
p1->x = 1;
p1->next = (struct p*)malloc(sizeof(struct p));
return 0;
}A.
free(p1);
free(p1->next)B.
free(p1->next);
free(p1);C.
free(p1);D. all of the mentioned
Answer: Option B

Join The Discussion