sizeof操作符的一些例子

 例一:

#include <iostream>
using namespace std;

class A
{
public:
    char c;
};

class B
{
public:
    int a;
    short b;
};

class C
{
public:
    int a;
    short b;
    char c;
};

class D
{
public:
    int a;
    short b;
    char c;
    char d;
};

class E
{
public:
    int a;
    short b;
    char c;
    char d;
    char e;
};

int main()
{
    cout<< "sizeof(A) = " <<sizeof(A) << endl;
    cout<< "sizeof(B) = " <<sizeof(B) << endl;
    cout<< "sizeof(C) = " <<sizeof(C) << endl;
    cout<< "sizeof(D) = " <<sizeof(D) << endl;
    cout<< "sizeof(E) = " <<sizeof(E) << endl;
    return 0;
}
View Code

输出:

这里是32位机器,sizeof(B)=8,sizeof(C)=8,sizeof(E)=12的原因是字节对齐,编译器会在最末一个成员之后填充字节。

例二:

#include <iostream>
using namespace std;

class Base
{
public:
    Base(int x) : a(x)
    {}
    void print()
    {
        cout << "Base" << endl;
    }
private:
    int a;
};

class Derived : public Base
{
public:
    Derived(int x) : Base(x - 1), b(x)
    {}
    void print()
    {
        cout << "Derived" << endl;
    }
private:
    int b;
};

class A
{
public:
    A(int x) : a(x)
    {}
    virtual void print()
    {
        cout << "A" << endl;
    }
private:
    int a;
};

class B : public A
{
public:
    B(int x) : A(x - 1), b(x)
    {}
    virtual void print()
    {
        cout << "B" << endl;
    }
private:
    int b;
};

int main()
{
    cout<< "sizeof(Base) = " <<sizeof(Base) << endl;
    cout<< "sizeof(Derived) = " <<sizeof(Derived) << endl;
    cout<< "sizeof(A) = " <<sizeof(A) << endl;
    cout<< "sizeof(B) = " <<sizeof(B) << endl;
    return 0;
}
View Code

输出:

sizeof(Base)=4,int占4字节,print()函数不占内存;

sizeof(Derived)=8,比Base多一个int成员;

sizeof(A)=8,int占4字节,但是含有虚函数,包含了一个隐含的虚表指针成员,占4字节,共计8字节;

sizeof(B)=12,比A多一个int成员,共计12字节;

这里可能会存在一个疑问:B中也存在一个指向虚函数表的指针,所以应该是16字节。

解释:虚表指针(vptr)是作为对象的一部分存在于对象的空间中,一个类只有一个虚表指针,所以A中的虚表指针和B中的虚表指针是同一个,所以是12字节。

例三:

#include <iostream>
using namespace std;

class A
{
};

class B
{
};

class C : public A, public B
{
};

class D : virtual public A
{
};

class E : virtual public A, virtual public B
{
};

class F : public D
{
};

class G
{
public:
    int a;
    static int b;
};

int main()
{
    cout<< "sizeof(A) = " <<sizeof(A) << endl;
    cout<< "sizeof(B) = " <<sizeof(B) << endl;
    cout<< "sizeof(C) = " <<sizeof(C) << endl;
    cout<< "sizeof(D) = " <<sizeof(D) << endl;
    cout<< "sizeof(E) = " <<sizeof(E) << endl;
    cout<< "sizeof(F) = " <<sizeof(F) << endl;
    cout<< "sizeof(G) = " <<sizeof(G) << endl;
    return 0;
}
View Code

输出:

sizeof(A) = 1,类A是空类,编译器会安插一个char空类,标记它的每一个对象,因此为1字节;
sizeof(B) = 1,同上;
sizeof(C) = 1,类C多重继承类A和类B,大小仍然为1字节;
sizeof(D) = 4,类D虚继承自A,编译器安插了一个指向父类的指针,大小为4字节;
sizeof(E) = 4,类E虚继承自A和B,因此它有指向父类A和父类B的指针,加起来为8字节。注意:在GNU编译器下编译字节是4字节;
sizeof(F) = 4,类F继承D,大小为4字节;
sizeof(G) = 4,类G有一个static成员,这个静态成员不在类的实例中,而是像全局变量一样在静态存储区中,被类G共享,所以只计算一个int成员,为4字节;

PS:如果在A中加入char a;

输出:sizeof(D) = 8 sizeof(E) = 8 sizeof(F) = 8 ,理由是字节对齐。

例四:

#include <iostream>
using namespace std;

union u1
{
    double a;
    int b;
};

union u2
{
    char a[13];
    int b;
};

union u3
{
    char a[13];
    char b;
};

int main()
{
    cout<< "sizeof(u1) = " <<sizeof(u1) << endl;
    cout<< "sizeof(u2) = " <<sizeof(u2) << endl;
    cout<< "sizeof(u3) = " <<sizeof(u3) << endl;
    return 0;
}
View Code

输出:

这里定义了三个联合体。联合体的大小取决于它所有成员中占用空间最大的一个成员的大小。对于符合数据类型,例如union,struct,class,对齐方式为成员中最大成员的对齐方式。

sizeof(u1) = 8,最大成员为double a,为8;
sizeof(u2) = 16,最大成员是char[13]的数组,但由于另一个成员int b,使u2的对齐方式变成4,所以u2的大小为4的倍数,所以占用的空间由13对齐成16;
sizeof(u3) = 13,最大成员是char[13]的数组,另一个成员char b,所以大小为13;

例五:(查看32位和64位机器字节大小)

#include <iostream>
using namespace std;

int main()
{
    cout<< "sizeof(char) = " <<sizeof(char) << endl;
        cout<< "sizeof(short) = " <<sizeof(short) << endl;
    cout<< "sizeof(int) = " << sizeof(int) <<endl;
    cout<< "sizeof(double) = " <<sizeof(double) << endl;
    cout<< "sizeof(long) = " <<sizeof(long) << endl;    
    cout<< "sizeof(longlong) = " <<sizeof(long long) << endl;
        
    cout<< "sizeof(void*) = " << sizeof(void*) <<endl;
    cout<< "sizeof(char*) = " << sizeof(char*) <<endl;
    
    cout<< "当前系统是sizeof(void*) * 8 = " << sizeof(void*)*8<<""<<endl;
    
    return 0;
}

32位输出:

64位输出:

sizeof(char) = 1
sizeof(short) = 2
sizeof(int) = 4
sizeof(double) = 8
sizeof(long) = 8
sizeof(longlong) = 8
sizeof(void*) = 8
sizeof(char*) = 8
当前系统是sizeof(void*) * 8 = 64位

说明:

1.long型变量在32位是4字节,64位是8字节

2.指针变量在32位是4字节,64位是8字节

原文地址:https://www.cnblogs.com/Brickert/p/10705656.html