Practice mode C Language

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.

Report a problem

Answer checked How we write and check questions

Tip: press A, B, C or D to answer, and N for the next question.

Advertisement

Have a question about this answer?

Your email address will not be published. Required fields are marked *

Links and website addresses are not allowed in comments.