Hi Guys,
Can someone please explain me why this is happening
#include<iostream>
using namespace std;
class a {
public:
int a1; // If I remove this it'll work fine
a() {
cout << "Constructor of a \n";
}
~a() {
cout << "Destructor of a \n";
}
};
class b : public a{
public:
b() {
cout << "constructor of b \n";
}
virtual ~b() { // If I remove virtual from here ... it'll work fine
cout << "destructor of b\n";
}
};
int main() {
a *p = new b;
delete p;
return 0;
}
Result is :
Constructor of a
constructor of b
Destructor of a
a.out(1563) malloc: *** error for object 0x100154: Non-aligned pointer
being freed
*** set a breakpoint in malloc_error_break to debug
----------------------------------------------------------------
I know that if I'll put virtual in front of the destructor of the base class code will work fine. But, I want to use it the above way. Can someone please explain me the reason behind this kind of behavior?
Thanks in advance