sizeof总结归纳

网上搜集了一些sizeof的使用,没事看看蛮有意思,整理如下:

///////////////////////////////////////////////////////////////////////////
    char str1[] = "hello";
    cout<<"str1-sizeof:"<<sizeof(str1)<<endl;        //=6 包括字符串结束符'\0'
    cout<<"str1-strlen:"<<strlen(str1)<<endl;        //=5 不包括字符串结束符'\0'

    char str2[5] = {'h','e','l','l','o'};            
    cout<<"str2-sizeof:"<<sizeof(str2)<<endl;        //=5 数组长度
    cout<<"str2-strlen:"<<strlen(str2)<<endl;        //=未知 无结束符'\0',strlen直到遇到'\0'停止

    char str3[6] = {'h','e','l','l','o','\0'};
    cout<<"str3-sizeof:"<<sizeof(str3)<<endl;        //=6 数组长度
    cout<<"str3-strlen:"<<strlen(str3)<<endl;        //=5

    char* str4 = "hello";
    cout<<"str4-sizeof:"<<sizeof(str4)<<endl;        //=4 指针变量长度
    cout<<"*str4-sizeof:"<<sizeof(*str4)<<endl;        //=1 'h'的长度
    cout<<"str4-strlen:"<<strlen(str4)<<endl;        //=5 字符串长度
    
    int ai[] = {1,2};
    cout<<"ai[]-sizeof:"<<sizeof(ai)<<endl;            //=8 数组长度×sizeof(int)
    
///////////////////////////////////////////////////////////////////////////

    double* (*a)[3][6];                                //3×6的二维数组,数组元素为指针,指向double型
    cout<<"a[]-sizeof:"<<sizeof(a)<<endl;            //=4 指向二维数组的指针
    cout<<"*a[]-sizeof:"<<sizeof(*a)<<endl;            //=4*3*6 此二维数组
    cout<<"**a[]-sizeof:"<<sizeof(**a)<<endl;        //=4*6 元素为double指针的一维数组
    cout<<"***a[]-sizeof:"<<sizeof(***a)<<endl;        //=4 一维数组第一个元素长度
    cout<<"****a[]-sizeof:"<<sizeof(****a)<<endl;    //=8 上述元素指向的double变量
    
///////////////////////////////////////////////////////////////////////////
    void acf(char p[3])
    {
        sizeof(p)                                    //=4 指针变量长度
    }
    
    void aif(int p[])
    {
        sizeof(p)                                    //=4 指针变量长度
    }
    
    void pif(int (*p)[6])
    {
        sizeof(p)                                    //=4 指针变量长度,指向int数组的指针
        sizeof(*p)                                    //=24 int数组的长度
    }
    
    void aif(int* p[6])
    {
        sizeof(p)                                    //=4 指向int指针的指针
        sizeof(*p)                                    //=4 指向int的指针
    }

///////////////////////////////////////////////////////////////////////////
    class CEmpty{};
    cout<<"CEmpty-sizeof:"<<sizeof(CEmpty)<<endl;    //空类占一个字节
    struct SEmpty{};
    cout<<"SEmpty-sizeof:"<<sizeof(SEmpty)<<endl;    //空结构体占一个字节

    class CInt
    {
        int i;
    };
    cout<<"CInt-sizeof:"<<sizeof(CInt)<<endl;        //=4 非空类和结构体所占字节数为所有成员占字节和(需考虑对齐),但不包括成员函数和静态成员所占空间。
    
    class CFunc
    {
        void f(){}
    };
    cout<<"CFunc-sizeof:"<<sizeof(CFunc)<<endl;        //=1

    //多重继承
    class A{};
    class B{};
    class C: public A, public B{};
    cout<<"C-sizeof:"<<sizeof(C)<<endl;                //=1 父子全空
    class D: virtual public A{};                
    cout<<"D-sizeof:"<<sizeof(D)<<endl;                //=4 虚继承时编译器为该类安插一个指向父类的指针
    class E: virtual public A, virtual public B{};
    cout<<"E-sizeof:"<<sizeof(E)<<endl;                //=8 两个父类指针
    
    //父类的私有数据
    class A
    {
    private:
        int a;
    };
    class B: public A{};
    cout<<"B-sizeof:"<<sizeof(B)<<endl;        //=4 父类的私有数据,子类不可用,但可见

    //虚函数
    class A
    {
        int a;
        void Function(){};
    };
    class B
    {
        int a;
        virtual void Function(){};
    };
    class C:public B
    {
        char b;
    };
    class D: public B
    {
        virtual void Function2(){};
    };
    cout<<"C-sizeof:"<<sizeof(C)<<endl;        //=12 包含一个虚函数表指针
    cout<<"D-sizeof:"<<sizeof(D)<<endl;        //=8 两个虚函数,但只有一个虚函数指针

//////////////////////////////////////////////////////////////
    struct s1
    {
        char a;
        double b;
        int c;
        char d;
    };
    cout<<"s1-sizeof:"<<sizeof(s1)<<endl;    //=24    

    struct s2
    {
        char a;
        char b;
        int c;
        double d;
    };
    cout<<"s2-sizeof:"<<sizeof(s2)<<endl;    //=16

    struct s1
    {
        char a[8];
    };

    struct s2
    {
        double d;
    };

    struct s3
    {
        s1 s;
        char a;
    };
    cout<<"s3-sizeof:"<<sizeof(s3)<<endl;    //=9 s1对齐方式为1

    struct s4
    {
        s2 s;
        char a;
    };
    cout<<"ss4-sizeof:"<<sizeof(ss4)<<endl;//=16 s2对齐方式为8

 

posted on 2015-05-08 09:19  MoZhao  阅读(237)  评论(0编辑  收藏  举报

导航