C++ 对象的内存布局(上)

简介:

对象的影响因素

简而言之,我们一个类可能会有如下的影响因素:

1)成员变量

2)虚函数(产生虚函数表)

3)单一继承(只继承于一个类)

4)多重继承(继承多个类)

5)重复继承(继承的多个父类中其父类有相同的超类)

6)虚拟继承(使用virtual方式继承,为了保证继承后父类的内存布局只会存在一份)

上述的东西通常是C++这门语言在语义方面对对象内部的影响因素,当然,还会有编译器的影响(比如优化),还有字节对齐的影响。在这里我们都不讨论,我们只讨论C++语言上的影响。

本篇文章着重讨论下述几个情况下的C++对象的内存布局情况。

1)单一的一般继承(带成员变量、虚函数、虚函数覆盖)

2)单一的虚拟继承(带成员变量、虚函数、虚函数覆盖)

3)多重继承(带成员变量、虚函数、虚函数覆盖)

4)重复多重继承(带成员变量、虚函数、虚函数覆盖)

5)钻石型的虚拟多重继承(带成员变量、虚函数、虚函数覆盖)

我们的目标就是,让事情越来越复杂。

知识复习

我们简单地复习一下,我们可以通过对象的地址来取得虚函数表的地址,如:

 typedef void(*Fun)(void);
 
            Base b;
 
            Fun pFun = NULL;
 
            cout << "虚函数表地址:" << (int*)(&b) << endl;
            cout << "虚函数表 — 第一个函数地址:" << (int*)*(int*)(&b) << endl;
 
            // Invoke the first virtual function 
            pFun = (Fun)*((int*)*(int*)(&b));
            pFun();

我们同样可以用这种方式来取得整个对象实例的内存布局。因为这些东西在内存中都是连续分布的,我们只需要使用适当的地址偏移量,我们就可以获得整个内存对象的布局。

本篇文章中的例程或内存布局主要使用如下编译器和系统:

1)Windows 7 和 VC++ 2010

2)Cygwin 和 G++ 3.4.4

单一的一般继承

下面,我们假设有如下所示的一个继承关系:

请注意,在这个继承关系中,父类,子类,孙子类都有自己的一个成员变量。而了类覆盖了父类的f()方法,孙子类覆盖了子类的g_child()及其超类的f()。

我们的源程序如下所示:

#include <iostream>
using namespace std;

class Parent {
public:
	int iparent;
	Parent ():iparent (10) {}
	virtual void f() { cout << " Parent::f()" << endl; }
	virtual void g() { cout << " Parent::g()" << endl; }
	virtual void h() { cout << " Parent::h()" << endl; }

};

class Child : public Parent {
public:
	int ichild;
	Child():ichild(100) {}
	virtual void f() { cout << "Child::f()" << endl; }
	virtual void g_child() { cout << "Child::g_child()" << endl; }
	virtual void h_child() { cout << "Child::h_child()" << endl; }
};

class GrandChild : public Child{
public:
	int igrandchild;
	GrandChild():igrandchild(1000) {}
	virtual void f() { cout << "GrandChild::f()" << endl; }
	virtual void g_child() { cout << "GrandChild::g_child()" << endl; }
	virtual void h_grandchild() { cout << "GrandChild::h_grandchild()" << endl; }
};

int _tmain(int argc, _TCHAR* argv[])
{
	typedef void(*Fun)(void);

	GrandChild gc;

	Fun pFun=NULL;

	int** pVtab = (int**)&gc;

	cout << "[0] GrandChild::_vptr->" << endl;
	/*for (int i=0; (Fun)pVtab[0][i]!=NULL; ++i){*/  
	//本人注释 测试环境win7+VS2010 经过调试发现此环境下虚函数并不是以NULL结尾,所以原文修改为如下:
	for (int i=0; i<6; ++i){
		pFun = (Fun)pVtab[0][i];
		cout << "    ["<<i<<"] ";
		pFun();
		
	}
	cout << "[1] Parent.iparent = " << (int)pVtab[1] << endl;
	cout << "[2] Child.ichild = " << (int)pVtab[2] << endl;
	cout << "[3] GrandChild.igrandchild = " << (int)pVtab[3] << endl;
 
	return 0;
}

image

使用图片表示如下:(红色部分为本人修改)

image

可见以下几个方面:

1)虚函数表在最前面的位置。

2)成员变量根据其继承和声明顺序依次放在后面。

3)在单一的继承中,被overwrite的虚函数在虚函数表中得到了更新。

多重继承

下面,再让我们来看看多重继承中的情况,假设有下面这样一个类的继承关系。注意:子类只overwrite了父类的f()函数,而还有一个是自己的函数(我们这样做的目的是为了用g1()作为一个标记来标明子类的虚函数表)。而且每个类中都有一个自己的成员变量:

我们的类继承的源代码如下所示:父类的成员初始为10,20,30,子类的为100

#include <iostream>
using namespace std;

class Base1 {
public:
	int ibase1;
	Base1():ibase1(10) {}
	virtual void f() { cout << "Base1::f()" << endl; }
	virtual void g() { cout << "Base1::g()" << endl; }
	virtual void h() { cout << "Base1::h()" << endl; }

};

class Base2 {
public:
	int ibase2;
	Base2():ibase2(20) {}
	virtual void f() { cout << "Base2::f()" << endl; }
	virtual void g() { cout << "Base2::g()" << endl; }
	virtual void h() { cout << "Base2::h()" << endl; }
};

class Base3 {
public:
	int ibase3;
	Base3():ibase3(30) {}
	virtual void f() { cout << "Base3::f()" << endl; }
	virtual void g() { cout << "Base3::g()" << endl; }
	virtual void h() { cout << "Base3::h()" << endl; }
};


class Derive : public Base1, public Base2, public Base3 {
public:
	int iderive;
	Derive():iderive(100) {}
	virtual void f() { cout << "Derive::f()" << endl; }
	virtual void g1() { cout << "Derive::g1()" << endl; }
};

int _tmain(int argc, _TCHAR* argv[])
{
	typedef void(*Fun)(void);

	Derive d;

	Fun pFun=NULL;

	int** pVtab = (int**)&d;

	cout << "[0] Base1::_vptr->" << endl;
	pFun = (Fun)pVtab[0][0];
	cout << "     [0] ";
	pFun();

	pFun = (Fun)pVtab[0][1];
	cout << "     [1] ";pFun();

	pFun = (Fun)pVtab[0][2];
	cout << "     [2] ";pFun();

	pFun = (Fun)pVtab[0][3];
	cout << "     [3] "; pFun();

	pFun = (Fun)pVtab[0][4];
	cout << "     [4] "; cout<<pFun<<endl;

	cout << "[1] Base1.ibase1 = " << (int)pVtab[1] << endl;


	int s = sizeof(Base1)/4;

	cout << "[" << s << "] Base2::_vptr->"<<endl;
	pFun = (Fun)pVtab[s][0];
	cout << "     [0] "; pFun();

	pFun = (Fun)pVtab[s][1];
	cout << "     [1] "; pFun();

	pFun = (Fun)pVtab[s][2];
	cout << "     [2] "; pFun();

	pFun = (Fun)pVtab[s][3];
	cout << "     [3] ";
	cout<<pFun<<endl;

	cout << "["<< s+1 <<"] Base2.ibase2 = " << (int)pVtab[s+1] << endl;

	s = s + sizeof(Base2)/4;

	cout << "[" << s << "] Base3::_vptr->"<<endl;
	pFun = (Fun)pVtab[s][0];
	cout << "     [0] "; pFun();

	pFun = (Fun)pVtab[s][1];
	cout << "     [1] "; pFun();

	pFun = (Fun)pVtab[s][2];
	cout << "     [2] "; pFun();

	pFun = (Fun)pVtab[s][3];
	cout << "     [3] ";
	cout<<pFun<<endl;

	s++;
	cout << "["<< s <<"] Base3.ibase3 = " << (int)pVtab[s] << endl;
	s++;
	cout << "["<< s <<"] Derive.iderive = " << (int)pVtab[s] << endl;

	return 0;
}

我们通过上面的程序来查看子类实例的内存布局:上面程序中,注意我使用了一个s变量,其中用到了sizof(Base)来找下一个类的偏移量。(因为我声明的是int成员,所以是4个字节,所以没有对齐问题。关于内存的对齐问题,大家可以自行试验,我在这里就不多说了)

image

使用图片表示是下面这个样子:

image

我们可以看到:

1) 每个父类都有自己的虚表。

2) 子类的成员函数被放到了第一个父类的表中。

3) 内存布局中,其父类布局依次按声明顺序排列。

4) 每个父类的虚表中的f()函数都被overwrite成了子类的f()。这样做就是为了解决不同的父类类型的指针指向同一个子类实例,而能够调用到实际的函数。

目录
相关文章
|
20天前
|
存储 编译器 C语言
C++入门: 类和对象笔记总结(上)
C++入门: 类和对象笔记总结(上)
30 0
|
24天前
|
存储 Java 编译器
C++:内存管理|new和delete
C++:内存管理|new和delete
|
23天前
|
存储 JSON 安全
【C++ JSON库 json值的创建手段】深入探究C++中JSON对象定位与操作:从引用到回调函数
【C++ JSON库 json值的创建手段】深入探究C++中JSON对象定位与操作:从引用到回调函数
60 0
|
18小时前
|
编译器 C++
自从学了C++之后,小雅兰就有对象了!!!(类与对象)(中)——“C++”
自从学了C++之后,小雅兰就有对象了!!!(类与对象)(中)——“C++”
|
18小时前
|
存储 编译器 C++
自从学了C++之后,小雅兰就有对象了!!!(类与对象)(上)——“C++”
自从学了C++之后,小雅兰就有对象了!!!(类与对象)(上)——“C++”
|
2天前
|
C++
【C++成长记】C++入门 | 类和对象(下) |Static成员、 友元
【C++成长记】C++入门 | 类和对象(下) |Static成员、 友元
|
2天前
|
存储 编译器 C++
【C++成长记】C++入门 | 类和对象(中) |拷贝构造函数、赋值运算符重载、const成员函数、 取地址及const取地址操作符重载
【C++成长记】C++入门 | 类和对象(中) |拷贝构造函数、赋值运算符重载、const成员函数、 取地址及const取地址操作符重载
|
20天前
|
编译器 C语言 C++
【c++】类和对象(三)构造函数和析构函数
朋友们大家好,本篇文章我们带来类和对象重要的部分,构造函数和析构函数
|
20天前
|
存储 编译器 C语言
【c++】类和对象(二)this指针
朋友们大家好,本节内容来到类和对象第二篇,本篇文章会带领大家了解this指针
【c++】类和对象(二)this指针
|
20天前
|
存储 编译器 C语言
【c++】类和对象(一)
朋友们,大家好,本篇内容我们来对类和对象进行初步的认识