← Copy Semantics: The Copy Constructor & Copy Assignment Operator

Fix: Shallow Copy Crash

hard

This Buffer has no copy constructor of its own – submit it as-is and look closely at the output. a.data[0] comes out 999, even though nothing ever wrote to a directly, and (if you run it enough times, or check the exit behavior) it eventually crashes with a double-free – exactly the lesson’s worked example, unmodified.

The fix is exactly what the previous exercises built: add a deep-copy copy constructor to Buffer, so Buffer b = a; gives b its own independent array instead of a second reference to a’s. Nothing else in this file needs to change – main never assigns to an existing Buffer here (there’s no b = a; with b already constructed), only the copy constructor is actually exercised.

Your solution

"Run" uses the sample stdin ("3"). "Submit" checks your code against all 3 test cases.

Test results

Submit your solution to run it against all test cases.

Reference solution