Practice mode C Language

When realloc() succeeds but moves the block to a new address, what happens to the old block?

Correct answer D. It is freed automatically after its contents are copied

Explanation

realloc may resize in place or allocate a new block. If it moves, it copies the old contents up to the smaller of the old and new sizes and then frees the old block itself. Any pointers to the old block become invalid, and freeing the old pointer again is a double free. Extra bytes in a larger block are not initialised.

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.