ProgrammingVery hard
What is the effect of realloc(p, 0) when p came from malloc, in the C standard's terms?
- A. It always frees p and returns NULL
- B. It always keeps p and returns it unchanged
- C. It is a compile error
- D. The behaviour is implementation-defined: the call may free p and return NULL, or return a unique pointer
Correct answer: D. The behaviour is implementation-defined: the call may free p and return NULL, or return a unique pointer
C99 made realloc with zero size implementation-defined, so portable code should not rely on either result. The option that always frees is not guaranteed.
More C Programming questions
- A function returns a pointer to a dynamically allocated copy of its argument string. Which signature is the most correct for the caller's…
- int m = malloc(3 sizeof m); for (int i = 0; i < 3; i++) m[i] = malloc(4 sizeof m); What is the number of allocation calls made in total?
- A doubly linked list node has prev and next. What must happen before freeing the node p in the middle of the list?
- A linked stack stores nodes with a pointer to the top. Push and pop run in O(1). What is the cost of freeing the whole stack when it has n…
- A list of 10^6 nodes is built by prepending, and then each node is looked up by value in a loop. What is the total cost of 10^6 lookups?
- int p = malloc(10 sizeof p); free(p); free(p); What is the result?