ProgrammingHardProduct company OA · GATE CS
Which of these statements about C's dynamic-memory functions are correct? (i) realloc(NULL, n) behaves like malloc(n). (ii) If realloc fails it returns NULL and frees the original block. (iii) free(NULL) does nothing. (iv) The memory returned by calloc is filled with zero bytes.
- A. (i), (iii) and (iv) only
- B. All four
- C. (i) and (iv) only
- D. (iii) and (iv) only
Correct answer: A. (i), (iii) and (iv) only
(ii) is the trap: when realloc fails it returns NULL and leaves the original block untouched and still allocated, which is why you assign its result to a temporary first. The other three are guaranteed by the standard.
More C Programming questions
- Why is the line p = realloc(p, 2 n sizeof p); considered unsafe?
- Given char s[] = "ab"; and the declarations below, for which of p1 to p4 does the statement p = 'x'; compile? c typedef char PC; const PC…
- Assume 32-bit unsigned int. What is printed? c unsigned int u = 1u << 31; printf("%u %u", u >> 31, (u >> 30) | (u >> 28 & 1));
- What is printed? c int x = 5, s = 0; switch (x) { default: s += 1; case 1: s += 10; case 2: s += 100; break; case 3: s += 1000; }…
- What is f in the declaration below? c int ((f)(int))[3];
- What is printed? c char s[10] = "abc"; char t[] = "xyz"; strncat(s, t, 2); printf("%s %zu %d", s, strlen(s), strncmp(s, "abcx", 4) == 0);