How will you free the memory allocated by the following program? #include…

2023

How will you free the memory allocated by the following program?

#include <stdio.h>
#include <stdlib.h>

#define MAXROW 3
#define MAXCOL 4

int main()
{
    int **p, i, j;
    p = (int **) malloc(MAXROW * sizeof(int*));
    return 0;
}
  1. A.

    memfree(int p);

  2. B.

    dealloc(p);

  3. C.

    malloc(p, 0);

  4. D.

    free(p);

Attempted by 531 students.

Show answer & explanation

Correct answer: D

Correct answer: free(p);

The only allocation in the given code is:

p = (int **) malloc(MAXROW * sizeof(int*));

This allocates memory for the array of row pointers only. Therefore, release that allocated block with:

free(p);

Important note: If memory had also been allocated for each row, then each row would have to be freed first, followed by free(p);. Since the given code does not allocate rows, only free(p); is required here.

A video solution is available for this question — log in and enroll to watch it.

Explore the full course: Mppsc Assistant Professor