I know that p
does not point to the starting address of the B
object, which causes the error of free()
function, but how to correct it?
Also, how do I use g++ to see the memory layout of a C++ class?
#include <iostream>
using namespace std;
class A {
public:
virtual void show() { cout << "A::show()" << endl; }
private:
int a;
};
class B : virtual public A {
public:
void show() { cout << "B::show()" << endl; }
private:
int b;
};
int main() {
A* p = new B();
p->show();
delete p;
return 0;
}
Output:
B::show()
free(): invalid pointer
Aborted (core dumped)
g++ 8.4.0