ProgrammingVery hard
Which declaration allocates a 3 x 4 int matrix as one contiguous block with a single free needed?
- A. int **m = malloc(3 * sizeof *m); for (...) m[i] = malloc(4 * sizeof **m);
- B. int *m[3]; m[0] = malloc(4 * sizeof **m);
- C. int m = malloc(12);
- 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
- A list of N nodes is freed with free() on each node but the next pointer is read after freeing it. Which order is correct?
- C(0) = 1 and C(n) = sum over i from 0 to n-1 of C(i) C(n-1-i). What is C(4)?
- A 3 x 3 grid holds 1 to 9 row by row, so row 0 is 1 2 3, row 1 is 4 5 6 and row 2 is 7 8 9. What is the sum of the 2 x 2 block with rows 1…
- In the sorted array {1, 2, 4, 6, 8, 9}, how many unordered pairs of distinct elements sum to 17?
- What is the largest sum of any three consecutive elements of {2, 1, 5, 1, 3, 2}?
- The sorted array is {1, 3, 6, 6, 8}. What is the index of the first occurrence of 6?