ProgrammingHard

int **m = malloc(3 * sizeof *m); for (int i = 0; i < 3; i++) m[i] = malloc(4 * sizeof **m); /* later */ for (int i = 0; i < 3; i++) free(m[i]); free(m); Which statement is true about this code?

  1. A. It frees every block exactly once, in the order rows then pointer array
  2. B. It leaks the pointer array because only rows are freed
  3. C. It frees m before the rows, which is undefined
  4. D. It frees each row twice

Correct answer: A. It frees every block exactly once, in the order rows then pointer array

Each row was allocated separately, so each is freed. The pointer array is freed last, after no row pointer is needed.

More C Programming questions

Exams that ask this topic