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);
- A. Block 1 (40 bytes)
- B. Block 2 (20 bytes)
- C. Block 3 (8 bytes)
- 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
- What is printed? c int pal(const char s, int i, int j) { if (i >= j) return 1; if (s[i] != s[j]) return 0; return pal(s, i + 1, j - 1); }…
- With 4-byte int, what is printed? c typedef int Row[3]; Row m[2] = {{1, 2, 3}, {4, 5, 6}}; Row p = m; printf("%zu %d", sizeof(Row)…
- What is printed? c int a = 0, b = 0, c = 0; if ((a = 2) && (b = 0) || (c = 3)) a++; printf("%d %d %d", a, b, c);
- Which line has undefined behaviour? c char a[] = "hi"; char p = "hi"; a[0] = 'H'; / (i) / p[0] = 'H'; / (ii) / char q = a; q[1] = 'I'; /…
- What is printed? c int calls = 0; int C(int n, int k) { calls++; return (k == 0 || k == n) ? 1 : C(n - 1, k - 1) + C(n - 1, k); } int…
- What is printed? c enum E { A = -2, B, C, D = A + 10, E2 }; printf("%d %d %d", B, C, E2);