ProgrammingModerateTCS NQT · Wipro
What does calloc(n, size) guarantee that malloc(n * size) does not?
- A. The allocated bytes are all set to zero
- B. The allocation is always faster
- C. The memory comes from the stack instead of the heap
- D. The memory is freed automatically when the function returns
Correct answer: A. The allocated bytes are all set to zero
calloc zero-fills the block; malloc's contents are indeterminate. Both blocks live until free is called, both come from the same allocator, and calloc may be slower because of the zeroing.
More C Programming questions
- What is printed? c int a[3][3] = {{1}, {2, 3}, [2][1] = 7}; printf("%d %d %d %d", a[0][1], a[1][1], a[2][0], a[2][1]);
- What does f(6) print? c void f(int n) { printf("%d", n); if (n > 1) f(n / 2); printf("%d", n); }
- What is printed? c sizet a = 3, b = 5; if (a - b > 0) puts("bigger"); else puts("smaller");
- What is printed? c int g(int a, int b) { return a == b ? a : a > b ? g(a - b, b) : g(a, b - a); } int main(void) { printf("%d", g(48…
- How many bytes does this code leak? c for (int i = 0; i < 3; i++) { char p = malloc(10); if (i == 1) free(p); }
- 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));