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);
}- A. 30
- B. 20
- C. 10
- 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
- Assume 4-byte float and 8-byte double. What is printed? c #define PI 3.14 printf("%zu %zu", sizeof(PI), sizeof(PI 2.0f));
- What is printed? c int f(int n) { static int calls; if (n == 0) { int c = calls; calls = 0; return c; } calls++; return f(n - 1); } int…
- What is printed? c enum Op { ADD, SUB, MUL }; int add(int a, int b) { return a + b; } int sub(int a, int b) { return a - b; } int mul(int…
- How many palindromic substrings does this code count? c const char s = "abba"; int n = 4, c = 0; for (int i = 0; i < n; i++) for (int j =…
- Assume 32-bit unsigned int. What is printed? c unsigned x = 0x27; / 10 0111 / printf("%u %u", ~x & (x + 1), x | (x + 1));
- What is printed? c int r0[] = {1, 2}, r1[] = {3, 4, 5}, r2[] = {6}; int rows[] = {r0, r1, r2}; int len[] = {2, 3, 1}; int s = 0; for (int…