为什么一个班级的大小取决于成员声明的顺序

为什么一个班级的大小取决于成员声明的顺序

本文介绍了为什么一个班级的大小取决于成员声明的顺序?如何?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人给我解释一下如何在类中的成员声明的顺序确定类的大小。

Someone explain me how does the order of the member declaration inside a class determines the size of that class.

例如:

class temp
{
public:
    int i;
    short s;
    char c;
};

以上班级的大小为8字节。

The size of above class is 8 bytes.

但是,当成员声明的顺序改变为以下

But when the order of the member declaration is changed as below

class temp
{
public:
    char c;
    int i;
    short s;
};

然后类的大小是12个字节。

then the size of class is 12 bytes.

如何?

推荐答案

背后的原因上述行为是数据结构调整和填充即可。基本上,如果你正在创建一个4字节变量如整型,它将被对齐到的 4字节边界即它将从在存储器中的地址,这是4 的多个启动。同样适用于其它数据类型。 2字节的短应从甚至内存地址启动等。

The reason behind above behavior is data structure alignment and padding. Basically if you are creating a 4 byte variable e.g. int, it will be aligned to a four byte boundary i.e. it will start from an address in memory, which is multiple of 4. Same applies to other data types. 2 byte short should start from even memory address and so on.

因此​​,如果你有INT之前声明的1字节字符(假设这里4个字节),会有之间留下3个免费字节。用他们的常用词是填充

Hence if you have a 1 byte character declared before the int (assume 4 byte here), there will be 3 free bytes left in between. The common term used for them is 'padded'

另一个好图案的解释

原因对齐

填充允许更快的内存访问,即对CPU,访问该对准内存区域快如读4字节对齐的整数可能需要的单个读取调用其中,仿佛一个整数位于非对齐地址范围(比如0x0002地址 - 0x0006),那么它会采取两个内存读取得到这个整数。

Padding allows faster memory access i.e. for cpu, accessing memory areas that are aligned is faster e.g. reading a 4 byte aligned integer might take a single read call where as if an integer is located at a non aligned address range (say address 0x0002 - 0x0006), then it would take two memory reads to get this integer.

迫使编译器,以避免调整的一种方式是(具体以GCC / G ++)使用关键字的包装的'同构的属性。 还链接指定如何执行校准使用您选择(2,4,8等)的特定边界的对齐的关键字。

One way to force compiler to avoid alignment is (specific to gcc/g++) to use keyword 'packed' with the structure attribute. packed keyword Also the link specifies how to enforce alignment by a specific boundary of your choice (2, 4, 8 etc.) using the aligned keyword.

最佳实践

它始终是一个好主意,组织你的类/结构的方式,变量已经与最小填充对齐。这降低了总的类的大小加上它减少工作由编译即,没有重排结构的完成的工作量。另外每个人都应该由他们的名字访问成员变量在code,而不是试图读取结构假设值将位于该字节中的特定字节。

It is always a good idea to structure your class/struct in a way that variables are already aligned with minimum padding. This reduces the size of the class overall plus it reduces the amount of work done by the compiler i.e. no rearrangement of structure. Also one should always access member variables by their names in the code, rather than trying to read a specific byte from structure assuming a value would be located at that byte.

另一个有用的SO上对齐的性能优势问题

Another useful SO question on performance advantage of alignment

有关完成起见,以下将的大小为8字节在您的情况还是有,但它不会因为获得的满8个字节,现在占用,并且没有填充

For the sake of completion, following would still have a size of 8 bytes in your scenario, but it won't get any better since full 8 bytes are now occupied, and there is no padding.

class temp
{
public:
    int i;
    short s;
    char c;
    char c2;
};

这篇关于为什么一个班级的大小取决于成员声明的顺序?如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 06:04