ProgrammingModerateProduct company OA

How many bytes does this code leak?

How many bytes does this code leak?
for (int i = 0; i < 3; i++) {
    char *p = malloc(10);
    if (i == 1) free(p);
}
  1. A. 30
  2. B. 20
  3. C. 10
  4. D. 0

Correct answer: B. 20

Each pass allocates 10 bytes and p goes out of scope at the end of the pass. Only the block from i = 1 is freed, so the blocks from i = 0 and i = 2 become unreachable: 20 bytes leaked.

More C Programming questions

Exams that ask this topic