C++ - 对象模型之内存布局(一)

1、基本内存布局

空类

#include <stdio.h>
class Child{
};

int main(){
     Child child1;
     Child child2;
     printf("child1 : address(0x%p) size(%d)\n", &child1,sizeof(child1));
     printf("child2 : address(0x%p) size(%d)\n", &child2,sizeof(child2));
     printf("child1.char : 0x%02x\n",child1);
     printf("child2.char : 0x%02x\n",child2);
}`
打印结果
child1 : address(0x0012FF63) size(1)
child2 : address(0x0012FF57) size(1)
child1.char : 0xcc
child2.char : 0xcc
内存布局
Child Size: 1B
|char|

无继承

#include <stdio.h>
class Child{
public:
     int age;
};
int main(){
     Child child1;
     printf("child1 : address(0x%p) size(%d)\n", &child1,sizeof(child1));
     printf("child1.age : address(0x%p)\n", &child1.age);
}
打印结果
child1 : address(0x0012FF60) size(4)
child1.age : address(0x0012FF60)
打印结果

内存布局
Child Size:4B
|age|

无继承&多态

#include <stdio.h>
typedef void(*Fun)(void);

class Child{
public:
     int age;
     Child():age(8){}
     virtual void who(){    printf("I am child\n");}
     virtual void study(){  printf("study...\n");}
};

int main(){
     Child child;
     Fun fun = NULL;
     int * vtbl = (int *)(*((int*)&child));

     printf("child(0x%p) : size(%d)\n",&child,sizeof(child));
     printf("child[0] is vptr, points to vtbl 0x%p\n",vtbl);
     printf("child[1] is Age : %d\n", ((int *)(&child))[1]);

     for (int i=0; (Fun)vtbl[i]!=NULL;i++){
         fun = (Fun)vtbl[i];
         printf("vtbl[%d] : ",i);
         fun();
     }
}
打印结果
child(0x0012FF5C) : size(8)
child[0] is vptr, points to vtbl 0x0041587C
child[1] is Age : 8
vtbl[0] : I am child
vtbl[1] : study...
内存布局
Child Size:8B
|vptr|age|
内存布局
Child Size:8B
|vptr|age|
其他
Vtbl存在堆上

单一继承

#include <stdio.h>
typedef void(*Fun)(void);

class GrandFather{
public:
     GrandFather():age(60){}
     void fishing(){    printf("GrandFather go to fishing...\n");}
     int age;
};

class Father:public GrandFather{
public:
     Father():age(36){}
     void cutting(){    printf("Father go to cutting...\n");}
     int age;
};

class Child:public Father{
public:
     Child():age(8){}
     void studying(){   printf("Child go to studying...\n");}
     int age;
};

int main(){
     Child child;
     printf("child(0x%p) : size(%d)\n",&child,sizeof(child));
     printf("child[0] is GrandFather : %d\n", ((int *)(&child))[0]);
     printf("child[1] is Father : %d\n", ((int *)(&child))[1]);
     printf("child[2] is child : %d\n", ((int *)(&child))[2]);
}
打印结果
child(0x0012FF58) : size(12)
child[0] is GrandFather : 60
child[1] is Father : 36
child[2] is child : 8
内存布局
Child Size:12B
|GrandFather::age|
|Father::age|
|Child::age|

单一继承&多态

#include <stdio.h>
typedef void(*Fun)(void);

class GrandFather{
public:
     GrandFather():age(60){}
     virtual void who(){    printf("I am GrandFather\n");}
     virtual void fishing(){ printf("GrandFather go to fishing...\n");}
     virtual void hungry(){ printf("GrandFather is hungry\n");}
     int age;
};

class Father:public GrandFather{
public:
     Father():age(36){}
     virtual void who(){    printf("I am Father\n");}
     virtual void cutting(){ printf("Father go to cutting...\n");}
     virtual void hungry(){ printf("Father is hungry\n");}
     int age;
};

class Child : public Father{
public:
     Child():age(8){}
     virtual void who(){    printf("I am Child\n");}
     virtual void studying(){    printf("Child go to studying...\n");}
     virtual void hungry(){ printf("Child is hungry\n");}
     int age;
};

int main(){

     Child child;
     Fun fun = NULL;
     int * vtbl = (int *)(*((int*)&child));

     printf("child(0x%p) : size(%d)\n",&child,sizeof(child));
     printf("child[0] is vptr, points to vtbl 0x%p\n",vtbl);
     printf("child[1] is GrandFather : %d\n", ((int *)  (&child[1]));
     printf("child[2] is Father : %d\n", ((int *)(&child))[2]);
     printf("child[3] is child : %d\n", ((int *)(&child))[3]);

     for (int i=0; (Fun)vtbl[i]!=NULL;i++){
         fun = (Fun)vtbl[i];
         printf("vtbl[%d] : ",i);
         fun();
     }
}
打印结果
child(0x0012FF54) : size(16)
child[0] is vptr, points to vtbl 0x004157FC
child[1] is GrandFather : 60
child[2] is Father : 36
child[3] is child : 8
vtbl[0] : I am Child
vtbl[1] : GrandFather go to fishing...
vtbl[2] : Child is hungry
vtbl[3] : Father go to cutting...
vtbl[4] : Child go to studying... 
内存布局
Child Size:16B 
|vptr|
|GrandFather::age|
|Father::age|
|Child::age|
Vtbl
|Child::who()|
|GrandFather::fishing()|
| Child::hungry()|
|Father::cutting()|
|Child::studying()|

多重继承

#include <stdio.h>

class GrandFather{
public:
         GrandFather():age(60){}
         int age;
};

class Grandad{
public:
         Grandad():age(57){}
         int age;
};

class Father:public GrandFather{
public:
         Father():age(36){}
         int age;
};

class Mother:public Grandad{
public:
         Mother():age(34){}
         int age;
};

class Child : public Father, public Mother{
public:
         Child():age(8){}
         int age;
};

int main(){

         Child child;
         int* pchild = (int *)&child;

         printf("child(0x%p) : size(%d)\n",&child,sizeof(child));
         printf("child[0] GrandFather : %d\n",pchild[0]);
         printf("child[1] Father : %d\n",pchild[1]);
         printf("child[2] Grandad : %d\n",pchild[2]);
         printf("child[3] Mother : %d\n",pchild[3]);
         printf("child[4] Child : %d\n",pchild[4]);
}
打印结果
child(0x0012FF50) : size(20)
child[0] : 60
child[1] : 36
child[2] : 57
child[3] : 34
child[4] : 8
内存布局
Child Size:20B
|GrandFather::age|
|Father::age|
|Grandad::age|
|Mother::age|
|Child::age|

多重继承&多态

#include <stdio.h>
typedef void(*Fun)(void);

class GrandFather{
public:
         GrandFather():age(60){}
         virtual void who(){   
             printf("I am GrandFather\n");
         }
         virtual void fishing(){  
             printf("GrandFather go to fishing...\n");
         }
         virtual void hungry(){       
             printf("GrandFather is hungry\n");
         }
         int age;
};

class Grandad{
public:
         Grandad():age(57){}
         virtual void who(){   
             printf("I am Grandad\n");
         }
         virtual void chessing(){   
             printf("Grandad go to chessing...\n");
         }
         virtual void hungry(){   
             printf("Grandad is hungry\n");
         }
         int age;
};

class Father:public GrandFather{
public:
         Father():age(36){}
         virtual void who(){   
             printf("I am Father\n");
         }
         virtual void cutting(){    
             printf("Father go to cutting...\n");
         }
         virtual void hungry(){     printf("Father is hungry\n");
         }
         int age;
};

class Mother:public Grandad{
public:
         Mother():age(34){}
         virtual void who(){   
             printf("I am Mother\n");
         }
         virtual void sewing(){     
             printf("Mother go to sewing...\n");
         }
         virtual void hungry(){    printf("Mother is hungry\n");
         }
         int age;
};

class Child : public Father, public Mother{
public:
         Child():age(8){}
         virtual void who(){   
             printf("I am Child\n");
         }
         virtual void studying(){   
             printf("Child go to studying...\n");
         }
         virtual void hungry(){    
             printf("Child is hungry\n");
         }
         int age;
};

int main(){

         Child child;
         int* pchild = (int *)&child;

         printf("child(0x%p) : size(%d)\n",&child,sizeof(child));
         printf("child[0] vptr1 : 0x%p\n",pchild[0]);
         printf("child[1] GrandFather : %d\n",pchild[1]);
         printf("child[2] Father : %d\n",pchild[2]);
         printf("child[3] vptr2 : 0x%p\n",pchild[3]);
         printf("child[4] Grandad : %d\n",pchild[4]);
         printf("child[5] Mother : %d\n",pchild[5]);
         printf("child[6] Child : %d\n",pchild[6]);

         Fun fun = NULL;
         int * vtbl1 = (int *)pchild[0];
         for (int i=0; (Fun)vtbl1[i]!=NULL; i++){
                   fun = (Fun)vtbl1[i];
                   printf("vtbl1[%d] : ", i);
                   fun();
         }

         int * vtbl2 = (int *)pchild[3];
         for (int i=0; (Fun)vtbl2[i]!=NULL; i++){
                   fun = (Fun)vtbl2[i];
                   printf("vtbl2[%d] : ", i);
                   fun();
         }
         getchar();
}
打印结果
child(0x0012FF48) : size(28)
child[0] vptr1 : 0x00416864
child[1] GrandFather : 60
child[2] Father : 36
child[3] vptr2 : 0x0041684C
child[4] Grandad : 57
child[5] Mother : 34
child[6] Child : 8
vtbl1[0] : I am Child
vtbl1[1] : GrandFather go to fishing...
vtbl1[2] : Child is hungry
vtbl1[3] : Father go to cutting...
vtbl1[4] : Child go to studying...
vtbl2[0] : I am Child
vtbl2[1] : Grandad go to chessing...
vtbl2[2] : Child is hungry
vtbl2[3] : Mother go to sewing...
内存布局
Child Size:28B
|vptr1|
|GrandFather::age|
|Father::age|
|vptr2|
|Grandad::age|
|Mother::age|
|Child::age|

猜你喜欢

转载自blog.csdn.net/qq_35433716/article/details/81742744