ProgrammingHardProduct company OA

With 4-byte int, after these lines run, which block can no longer be freed because no pointer to it remains?

With 4-byte int, after these lines run, which block can no longer be freed because no pointer to it remains?
int *a = malloc(10 * sizeof(int));   /* block 1 */
int *b = malloc(5 * sizeof(int));    /* block 2 */
a = b;
b = malloc(2 * sizeof(int));         /* block 3 */
free(a);
  1. A. Block 1 (40 bytes)
  2. B. Block 2 (20 bytes)
  3. C. Block 3 (8 bytes)
  4. D. None: every block is still reachable or freed

Correct answer: A. Block 1 (40 bytes)

a = b overwrites the only pointer to block 1, so it is leaked. a now points to block 2, which free(a) releases. Block 3 is still reachable through b and can be freed later.

More C Programming questions

Exams that ask this topic