← Destructors & Object Lifetime

new, delete, and What Happens Without It

hard

Two Resource objects are allocated with new. Delete only a, then print done – leave b un-deleted, deliberately, so its destructor’s silence is visible in the expected output.

Look closely at the expected output: Destroyed: beta never appears. That’s the actual point of this exercise, not an oversight – b’s destructor genuinely never runs, because nothing ever calls delete on it. In a real program this is exactly a memory leak, the C++ equivalent of forgetting free in Module 2, just with a constructor/destructor pair making the consequence directly visible here instead of invisible. Every new still needs exactly one matching delete, precisely as Module 2 required for every malloc and free – this exercise just makes what “forgetting” costs impossible to miss.

Your solution

"Run" uses the sample stdin (none). "Submit" checks your code against all 1 test case.

Test results

Submit your solution to run it against all test cases.

Hints

    Reference solution