← Quiz: Dynamic Memory: all 10 questions
Quiz: Dynamic Memory · Question 7 of 10
Why is p = realloc(p, newSize); risky?
Why is p = realloc(p, newSize); risky?
- A.realloc always moves the data
- B.If realloc fails it returns NULL, and the only pointer to the old block is lost, leaking it
- C.realloc frees p twice
- D.realloc cannot make a block bigger
Answer & explanation
Correct answer: If realloc fails it returns NULL, and the only pointer to the old block is lost, leaking it
Store the result in a temporary pointer first, and assign it to p only if it is not NULL.
Can you get the rest right without peeking?
Take the timed Quiz: Dynamic Memory quiz free. Pass it to earn XP and count towards your certificate.
Start the quiz →