Why is writing p = realloc(p, n); considered risky?
Correct answer C. If realloc fails it returns NULL, overwriting p and leaking the original block
Explanation
When realloc fails it returns NULL but leaves the original block allocated and unchanged. Assigning that NULL straight back to p loses the only pointer to the block, causing a leak and losing the data. The safe pattern stores the result in a temporary: tmp = realloc(p, n); if (tmp != NULL) p = tmp; and otherwise handles the error while p is still valid.
Answer checked How we write and check questions
Discussion