Destructor role in C++ - Time & Space Complexity
We want to understand how the time cost of a destructor grows as the program runs.
Specifically, how the destructor's work changes when cleaning up objects.
Analyze the time complexity of this destructor code.
class MyClass {
std::string* data;
int size;
public:
MyClass(int n) : size(n) { data = new std::string[size]; }
~MyClass() { delete[] data; }
};
This code creates an array and the destructor frees that array when the object is destroyed.
Look at what the destructor does repeatedly.
- Primary operation: Deleting an array of size n.
- How many times: Once per object destruction, but the delete[] frees all elements.
The destructor frees memory proportional to the array size.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 steps to free |
| 100 | About 100 steps to free |
| 1000 | About 1000 steps to free |
Pattern observation: The work grows directly with the size of the array.
Time Complexity: O(n)
This means the destructor takes longer if the array it frees is bigger, growing in a straight line with size.
[X] Wrong: "Destructor runs instantly no matter the data size."
[OK] Correct: The destructor must free all allocated memory, so if the data is large, it takes more time.
Knowing how destructors scale helps you write efficient cleanup code and understand program performance better.
"What if the destructor also had to delete multiple arrays inside the object? How would the time complexity change?"
Practice
Solution
Step 1: Understand the purpose of a destructor
A destructor is a special function that runs when an object is destroyed to free resources like memory or files.Step 2: Compare with other options
Creating objects, initializing variables, and overloading operators are not roles of destructors.Final Answer:
To clean up resources when an object is destroyed -> Option AQuick Check:
Destructor role = cleanup [OK]
- Confusing destructor with constructor
- Thinking destructor creates objects
- Believing destructor initializes variables
Car?Solution
Step 1: Recall destructor syntax
A destructor has the same name as the class with a tilde (~) prefix and no return type.Step 2: Check each option
~Car() {}matches the correct syntax.void ~Car() {}wrongly includes 'void'.Car~() {}has wrong order.void Car() {}lacks tilde and looks like a constructor.Final Answer:
~Car() {} -> Option CQuick Check:
Destructor syntax = ~ClassName() [OK]
- Adding return type to destructor
- Placing tilde after class name
- Confusing destructor with constructor syntax
class Box {
public:
Box() { std::cout << "Created\n"; }
~Box() { std::cout << "Destroyed\n"; }
};
int main() {
Box b;
return 0;
}Solution
Step 1: Trace object creation
Whenbis created, constructor prints "Created".Step 2: Trace object destruction
At end ofmain(),bis destroyed, destructor prints "Destroyed".Final Answer:
Created\nDestroyed\n -> Option BQuick Check:
Constructor then destructor output [OK]
- Assuming destructor runs before constructor
- Expecting no output
- Confusing order of prints
class Sample {
public:
~Sample(int x) { }
};Solution
Step 1: Check destructor parameters
Destructors cannot have parameters; they must have empty parentheses.Step 2: Verify other rules
Destructor has no return type, must have ~ prefix, and cannot be static.Final Answer:
Destructor cannot have parameters -> Option AQuick Check:
Destructor params = none [OK]
- Adding parameters to destructor
- Adding return type
- Removing ~ from destructor name
FileHandler that opens a file in its constructor. Which is the best way to ensure the file is closed properly when the object is no longer needed?Solution
Step 1: Understand resource management
Files opened by an object should be closed when the object is destroyed to avoid resource leaks.Step 2: Use destructor for cleanup
Destructor runs automatically when object ends, so closing the file there ensures proper cleanup.Final Answer:
Write a destructor that closes the file -> Option DQuick Check:
Destructor cleans resources automatically [OK]
- Forgetting to close files causing leaks
- Relying on manual close calls
- Assuming OS always cleans up immediately
Ready to prove it?
Pass the test and this pattern moves unknown
