Examveda

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;
}

A.

free(p1);
free(p1->next)

B.

free(p1->next);
free(p1);

C.

free(p1);

D. all of the mentioned

Answer: Option B


This Question Belongs to C Program >> File Input Output

Join The Discussion

Related Questions on File Input Output