注册 登录  
 加关注
查看详情
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

一路

To find the final symmetry & beauty

 
 
 

日志

 
 
 
 

[C++ tips 1]查看类内存布局结构  

2010-09-17 10:32:36|  分类: tips |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

vs2005和以后的visual studio版本提供了一个可以查看类内存布局的编译选项,格式如下

cl [filename].cpp /d1reportSingleClassLayout[className]

比如对于如下代码

#include<iostream>

#include<cstdlib>

#include<cstdio>

#include<string>

using namespace std;

//Fonction to show binary info of an object

typedef unsigned char* pointer_type;

void show_byte(pointer_type pointer,int length);

void show_byte(pointer_type pointer,int length)

{

    cout<<"address of object:"<<hex<<(int)pointer<<endl;

    cout<<"var size in byte:"<<dec<<length<<endl;

    for(int i=0;i<length;++i)

    {

        cout<<"|address: 0x"<<hex<<(int)(pointer+i)<<" | value:0x"<<(int)(*(pointer+i))<<" |"<<endl;

    }

    cout<<endl;

}

class Base

{

public:

    Base()

    {

        cout<<"In Base::constructor."<<endl;

        cout<<"Constructing "<<*m_pObj_Name<<endl;

        cout<<"binary info:"<<endl;

        show_byte((pointer_type)this,sizeof(*this));

    }

    Base(const string& name):

        flag_var(0x12345678),

        m_pObj_Name(new string(name))

    {  

        cout<<"In Base::constructor."<<endl;

        cout<<"Constructing "<<*m_pObj_Name<<endl;

        cout<<"binary info:"<<endl;

        show_byte((pointer_type)this,sizeof(*this));

    }

    void Util()

    {

        virtual_func1();

    }

    virtual ~Base()  {delete m_pObj_Name;}

    const string& GetName()

    {

        return *(this->m_pObj_Name);

    }

private:

    int flag_var;

    string* m_pObj_Name;

    virtual void pure_virtual_1() = 0;//pure virtual function

    virtual void pure_virtual_2() = 0;//pure virtual function

    virtual void virtual_func1()      //normal virtual function

    {

        cout<<"virutal function in Base."<<endl;

    }

};

class Derived: public Base

{

public:

    Derived(string name):

            Base(name)

    {

        cout<<"In Derived::constructor."<<endl;

        cout<<"binary info:"<<endl;

        show_byte((pointer_type)this,sizeof(*this));

    }

    void Util()

    {

        virtual_func1();

    }

    virtual ~Derived()  {}

private:

        virtual void pure_virtual_1() { printf("pure_virtual_1() in drived\n"); }

        virtual void pure_virtual_2() { printf("pure_virtual_2() in drived\n"); }

        virtual void virtual_func1()

        {

            cout<<"virutal function in Derived."<<endl;

        }

};

int main()

{

    Derived* d1 = new Derived("Derived_1");

    Base* b1 = d1;

    cout<<"After construct:"<<d1->GetName()<<endl;

    show_byte((pointer_type)d1,sizeof(d1));

    d1->Util();

    b1->Util();

    Derived* d2 = new Derived("Derived_2");

    Base* b2 = d2;

    cout<<"After construct:"<<d2->GetName()<<endl;

    show_byte((pointer_type)d2,sizeof(d2));

    d2->Util();

    b2->Util();

    delete d1;

    delete d2;

    return 0;

}

 

采用如下编译指令:

cl derive.cpp /d1reportSingleClassLayoutDerived

有如下结果

class Derived   size(12):
        +---
        | +--- (base class Base)
 0      | | {vfptr}
 4      | | flag_var
 8      | | m_pObj_Name
        | +---
        +---

Derived::$vftable@:
        | &Derived_meta
        |  0
 0      | &Derived::{dtor}
 1      | &Derived::pure_virtual_1
 2      | &Derived::pure_virtual_2
 3      | &Derived::virtual_func1

Derived::{dtor} this adjustor: 0
Derived::pure_virtual_1 this adjustor: 0
Derived::pure_virtual_2 this adjustor: 0
Derived::virtual_func1 this adjustor: 0
Derived::__delDtor this adjustor: 0
Derived::__vecDelDtor this adjustor: 0

  评论这张
 
阅读(1485)| 评论(0)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018