Is the following code correct?
class A
{
public:
    int* m_a;
    A(int x) { m_a = new int(x);}
    ~A() {delete m_a;}
};

void f(A a)
{
    cout<<a.m_a;
}


int main()
{
    A a(1);
    f(a);
}
Explanation
The destructor is called twice, the first time when the function f() is exited, the second time it is called when the main() function is exited, trying to delete the memory allocated for the m_a variable again.

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback
Cosmo
Sign Up Now
or Subscribe for future quizzes