How to determine the size of a class/struct in the C++?

本文探讨了在C++编程中如何确定类和结构体的大小,并解释了内存对齐规则及其对性能的影响。通过具体示例展示了不同类型的变量如何影响整体大小。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

http://www.ibm.com/developerworks/library/pa-dalign/


To determine the size of a class/struct is important in programming with C++. A instant example of this issue is to calculate the pointer of the next item in an array of objects (adding the offset value to the initial pointer). The mechanism of determining the size comes from the rules of C, and expand to that of C++. The basic idea is to find out a calculating unit (which should be a multiple of 4, i.e., 1, 2, 4, 8, 16, ...), and then read in a unit each time until the .

The reason why C/C++ use this method is to sped up the read in. Use a (calculating) unit will save the time of calculating address.

Essentially, sizeof is an operator but not a function!!!It will return zero of an array which is not declared as a fixed size.

Description:

1. Find the largest system defined variable, and set it as the read-in unit.

2. The program will read in data based on the unit. (i.e. if double (8 bytes) is a unit, every time the program will readin 8 bytes.)

3. If a variable is not coincides with multiplicity of the unit, make up some bytes. The make-up bytes will be discarded after being read in. (i.e. char name[7]; is of size 7, and the unit is 8. Then, make up 1 byte so as to they can be read in once.)

4. About Member functions: member functions will not take size of a struct/class.

5. About "virtual" method: C++ will add a pointer (ess. an integer taking 4 bytes) to each virtual function; C++ will add a pointer to the base class, if the inherience is virtual.

6. Use #pragma pack(n) to set a new alignment method. (0, default method; 1 one byte; and so on).

Example code:

typedef struct test_struct{
    int m;
    int m_1;
    int m_2;
    double n;
    char name[7];
} test_struct;

class BaseClass
{
public:
    BaseClass(int input) {}
    BaseClass(float input) {}
    BaseClass(double input) {}
    ~BaseClass() {}
    virtual void Print() {}
private:
    double m_double;
    int m_int;
    int m_int_1;
    char name[11];
};

class DerivedClass : public BaseClass{
private:
    int d_m;
};

class DerivedClassVirtual : virtual public BaseClass{
private:
    int d_m;
};

class Wrapper{
private:
    DerivedClass m_object;
    int m_size;
};

int main()
{
    enum eDay {Mon = 1, Tue};
    cout << "Size of bool = " << sizeof(bool) << endl;  //1 bit, take the lowest position of a 8-bit array.
    cout << "Size of char = " << sizeof(char) << endl;
    cout << "Size of int = " << sizeof(int) << endl;
    cout << "Size of long = " << sizeof(long) << endl;
    cout << "Size of float = " << sizeof(float) << endl;
    cout << "Size of double = " << sizeof(double) << endl;
    cout << "Size of long double = " << sizeof(long double) << endl;
    cout << "Size of char array with 10 elements = " << sizeof(char[10]) << endl; //=sizeof(char) (=1) * 10
    cout << "Size of int array with 10 elements = " << sizeof(int[10]) << endl; //=sizeof(int) (=4) * 10
    cout << "======================================" << endl;
    cout << "Size of struct = " << sizeof(test_struct) << endl;
    cout << "Size of enum = " << sizeof(eDay) << endl;
    cout << "Size of base class = " << sizeof(BaseClass) << endl;
    cout << "Size of derived class = " << sizeof(DerivedClass) << endl;
    cout << "Size of virtual derived class = " << sizeof(DerivedClassVirtual) << endl;
    cout << "Wrapper size = " << sizeof(Wrapper) << endl;
    return 0;
}
Sample output:

Size of bool = 1
Size of char = 1
Size of int = 4
Size of long = 4
Size of float = 4
Size of double = 8
Size of long double = 12
Size of char array with 10 elements = 10
Size of int array with 10 elements = 40
======================================
Size of struct = 32
Size of enum = 4
Size of base class = 40
Size of derived class = 40
Size of virtual derived class = 48
Wrapper size = 48

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值