Rotate image (square matrix) by 90 deg
Tue, 06 May 2025
When exit(0) is used to exit from a C++ program, the destructor for local non-static objects is not called.
class MyClass
{
public:
// Constructor
MyClass() { cout<< "Constructor" << endl; }
// Destructor
MyClass() { cout<< "Destructor" << endl; }
};
int main()
{
MyClass obj;
// Using exit
exit(0);
}
Output:
Constructor
If we do a return from the main, then the implementation will make sure that the destructor is called
class MyClass
{
public:
// Constructor
MyClass() { cout<< "Constructor" << endl; }
// Destructor
MyClass() { cout<< "Destructor" << endl; }
};
int main()
{
MyClass obj;
// Using return
return 0;
}
Output:
Constructor Destructor
For static and global variables, the destructor may get called (depending on the compiler implementation).
class MyClass
{
public:
// Constructor
MyClass() { cout<< "Constructor" << endl; }
// Destructor
MyClass() { cout<< "Destructor" << endl; }
};
int main()
{
static MyClass obj;
// Using exit
exit(0);
}
Output:
Implementation Dependent
This is because static & global variables are load-time variables and are allocated memory in the data area (which is independent of function calls) and not in the Activation Record of a function.
Load time variables are cleaned up by the start-up library.
Tue, 06 May 2025
Tue, 06 May 2025
Tue, 06 May 2025
Leave a comment