ProgrammingVery hard

Which declaration allocates a 3 x 4 int matrix as one contiguous block with a single free needed?

  1. A. int **m = malloc(3 * sizeof *m); for (...) m[i] = malloc(4 * sizeof **m);
  2. B. int *m[3]; m[0] = malloc(4 * sizeof **m);
  3. C. int m = malloc(12);
  4. D. int *m = malloc(3 * 4 * sizeof *m);

Correct answer: D. int *m = malloc(3 * 4 * sizeof *m);

One malloc gives one block, so one free releases it all. Separate row allocations need one free per row.

More C Programming questions

Exams that ask this topic