Home » C Programming » Storage Classes » Question
  1. 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 n;
    };
    int main()
    {
    struct p *ptr1 = (struct ptr*)malloc(sizeof(struct p));
    ptr1->n = 1;
    ptr1->next = (struct ptr*)malloc(sizeof(struct p));
    return 0;
    }
    1. free(ptr1);
    2. free(ptr1);
      free(ptr1->next)
    3. free(ptr1->next);
      free(ptr1);
    4. All of above
    5. None of these
Correct Option: C

free(ptr1->next);
free(ptr1);



Your comments will be displayed only after manual approval.