本文主要以示例说明C++对象数据成员存储布局,有关理论知识,请参考Data语意学。
一、数据成员的布局
1.1、无继承
#include <iostream>
using namespace std;
class X
{
public:
int a = 0;
static int staticx;
int b = 1;
};
int X::staticx = 42;
class Y
{
public:
int a = 0;
static int staticy;
public:
int b = 1;
};
int Y::staticy = 43;
int main()
{
X x;
cout << "X:" << endl;
cout << "sizeof(X) = " << sizeof(X) << endl;
cout << "vptr address: " << (&x) << endl;
cout << "x.a address: " << &x.a << endl;
cout << "x.b address: " << &x.b << endl;
cout << "x.staticx address: " << &x.staticx << endl;
Y y;
cout << "\nY:" << endl;
cout << "sizeof(Y) = " << sizeof(Y) << endl;
cout << "vptr address: " << (&y) << endl;
cout << "y.a address: " << &y.a << endl;
cout << "y.b address: " << &y.b << endl;
cout << "y.staticy address: " << &y.staticy << endl;
return 0;
}
程序运行结果如下:
从上述结果可以看出:非静态数据成员在类对象中的排列顺序将和其被声明的顺序一样,任何中间介入的静态数据成员都不会被放进对象布局之中。在上述例子中,X和Y对象是由两个int组成的,顺序是a、b。
C++标准要求,在同一个access section(也就是private、public、protected等区段)中,成员的排列只需符合"较晚出现的成员在类对象中有较高的地址"这一条即可。
C++编译器也允许编译器将多个access section之中的数据成员自由排列,不必在乎它们出现在类声明中的顺序。
1.2、单一继承
#include <iostream>
using namespace std;
class X
{
public:
int ax = 0;
static int staticx;
int bx = 1;
char cx = 'a';
};
int X::staticx = 42;
class Y :public X
{
public:
int ay = 0;
static int staticy;
public:
int by = 1;
};
int Y::staticy = 43;
int main()
{
X x;
cout << "X:" << endl;
cout << "sizeof(X) = " << sizeof(X) << endl;
cout << "x address: " << (&x) << endl;
cout << "x.ax address: " << &x.ax << endl;
cout << "x.bx address: " << &x.bx << endl;
cout << "x.cx address: " << &x.cx << endl;
cout << "x.staticx address: " << &x.staticx << endl;
Y y;
cout << "\nY:" << endl;
cout << "sizeof(Y) = " << sizeof(Y) << endl;
cout << "y address: " << (&y) << endl;
cout << "y.ax address: " << &y.ax << endl;
cout << "y.bx address: " << &y.bx << endl;
cout << "y.cx address: " << &y.cx << endl;
cout << "y.ay address: " << &y.ay << endl;
cout << "y.by address: " << &y.by << endl;
cout << "y.staticy address: " << &y.staticy << endl;
return 0;
}
程序运行结果如下:
从上述结果可以看出:在X中,为了对其内存,会填补额外的3个字节。在Y中,首先存放的是X的子对象的数据成员,然后存放Y的数据成员。
X和Y的内存布局如下:
1.3、多重继承
#include <iostream>
using namespace std;
class X
{
public:
int ax = 0;
static int staticx;
int bx = 1;
char cx = 'a';
};
int X::staticx = 42;
class Y
{
public:
int ay = 0;
static int staticy;
public:
int by = 1;
};
int Y::staticy = 43;
class M :public X, public Y
{
public:
int am = 0;
};
int main()
{
X x;
cout << "X:" << endl;
cout << "sizeof(X) = " << sizeof(X) << endl;
cout << "x address: " << &x << endl;
cout << "x.ax address: " << &x.ax << endl;
cout << "x.bx address: " << &x.bx << endl;
cout << "x.cx address: " << &x.cx << endl;
cout << "x.staticx address: " << &x.staticx << endl;
Y y;
cout << "\nY:" << endl;
cout << "sizeof(Y) = " << sizeof(Y) << endl;
cout << "y address: " << &y << endl;
cout << "y.ay address: " << &y.ay << endl;
cout << "y.by address: " << &y.by << endl;
cout << "y.staticy address: " << &y.staticy << endl;
M m;
cout << "\nM:" << endl;
cout << "sizeof(M) = " << sizeof(M) << endl;
cout << "m address: " << &m << endl;
cout << "m.ax address: " << &m.ax << endl;
cout << "m.bx address: " << &m.bx << endl;
cout << "m.cx address: " << &m.cx << endl;
cout << "m.ay address: " << &m.ay << endl;
cout << "m.by address: " << &m.by << endl;
cout << "m.staticx address: " << &m.staticx << endl;
cout << "m.staticy address: " << &m.staticy << endl;
cout << "m.am address: " << &m.am << endl;
return 0;
}
程序运行结果如下:
从上述结果可以看出:在M中,首先存放的是最左边的基类(也就是第一个基类)X的子对象的数据成员,然后存放Y的数据成员,最后存放M的数据成员。
M的内存布局如下:
1.4、虚继承
1.4.1、单一虚继承
#include <iostream>
using namespace std;
class X
{
public:
int ax = 0;
};
class Y :virtual public X
{
public:
int ay = 0;
};
class Z :virtual public X
{
public:
virtual void funz() { cout << "Z::funx()" << endl; }
int az = 0;
};
int main()
{
X x;
cout << "X:" << endl;
cout << "sizeof(X) = " << sizeof(X) << endl;
cout << "x address: " << (&x) << endl;
cout << "x.ax address: " << &x.ax << endl;
Y y;
cout << "\nY:" << endl;
cout << "sizeof(Y) = " << sizeof(Y) << endl;
cout << "y address: " << (&y) << endl;
cout << "y.ay address: " << &y.ay << endl;
cout << "y.ax address: " << &y.ax << endl;
Z z;
cout << "\nY:" << endl;
cout << "sizeof(Z) = " << sizeof(Z) << endl;
cout << "z address: " << (&z) << endl;
cout << "z.az address: " << &z.az << endl;
cout << "z.ax address: " << &z.ax << endl;
return 0;
}
程序运行结果如下:
从上述结果可以看出:在Y中,首先存放的是虚基类表指针,然后存放Y中的数据成员,最后存放虚基类X的数据成员。在Z中,首先存放的是虚函数表指针,然后是虚基类表指针,接下来存放Z中的数据成员,最后存放虚基类X的数据成员。
x、y、z的内存布局如下:
1.4.2、多重虚继承
#include <iostream>
using namespace std;
class X
{
public:
int ax = 0;
};
class Y :virtual public X
{
public:
virtual void funy() { cout << "Y::funy" << endl; }
int ay = 0;
};
class Z :virtual public X
{
public:
int az = 0;
};
class M :public Y, public Z
{
public:
virtual void funm() { cout << "M::funm()" << endl; }
int am = 0;
};
int main()
{
X x;
cout << "X:" << endl;
cout << "sizeof(X) = " << sizeof(X) << endl;
cout << "x address: " << (&x) << endl;
cout << "x.ax address: " << &x.ax << endl;
Y y;
cout << "\nY:" << endl;
cout << "sizeof(Y) = " << sizeof(Y) << endl;
cout << "y address: " << (&y) << endl;
cout << "y.ay address: " << &y.ay << endl;
cout << "y.ax address: " << &y.ax << endl;
Z z;
cout << "\nZ:" << endl;
cout << "sizeof(Z) = " << sizeof(Z) << endl;
cout << "z address: " << (&z) << endl;
cout << "z.az address: " << &z.az << endl;
cout << "z.ax address: " << &z.ax << endl;
M m;
cout << "\nM:" << endl;
cout << "sizeof(M) = " << sizeof(M) << endl;
cout << "m address: " << (&m) << endl;
cout << "m.ay address: " << &m.ay << endl;
cout << "m.az address: " << &m.az << endl;
cout << "m.am address: " << &m.am << endl;
cout << "m.ax address: " << &m.ax << endl;
return 0;
}
程序运行结果如下:
从上述结果可以看出:在M中,首先存放的是最左边的基类(即第一个基类)的子对象,需要注意的是:M中的虚函数地址存放在Y的子对象的虚函数表中,接下来依次存放其他基类子对象,然后是M的数据成员,最后存放虚基类X的子对象。
x、y、z、m的内存布局如下
二、数据成员指针
#include <iostream>
#include <cstdio>
using namespace std;
class X
{
public:
int ax = 0;
int bx = 1;
void printfun()
{
printf("&X::ax = %p\n", &X::ax);
printf("&X::bx = %p\n", &X::bx);
}
};
int main()
{
X x;
x.printfun();
return 0;
}
程序运行结果如下: