Memory Alignment: bit segment size


The reason memory alignment

  1. Platform reason: Not all hardware platforms can access any data on any address; some hardware platforms can take only certain types of data at a certain address, hardware or throw an exception.
  2. For performance reasons: when unaligned memory access can be saved, equivalent to that memory alignment is holding space for time approach.

Align the memory structure is to hold the space for time approach.

Memory alignment rules

  1. A first member and a structure variable offset amount is 0 at the address.
  2. Other members of the variables to be aligned to the address of an integer multiple digital (alignment number). Alignments = compiler small value a default size and the number of aligned members. VS aligned default number 8, the default number of Linux 4 are aligned.
  3. The total size of the structure is a maximum number (except the first member variable each member has a number of alignment) of an integral multiple alignment.
  4. If the nested structure of the situation, the number of aligned nested structure is the maximum alignment own member variables .

Empty class of computing

Empty class size is: a byte, used placeholder.
Write pictures described here

The size of the nested structure

struct Node1{
	int a;			
	char c;
};
struct Node2
{
	int data;		   //对齐数是0,大小是4
	struct Node1 node;//默认对齐数是Node1的成员最大对齐数4,大小是8
};
int main()
{
	cout<<sizeof(Node2) << endl;//结果是12
}

Chestnut illustrate the design principles of structure

  • Let the members of the small footprint as much as possible together.
struct A
{					  		 //对齐数	至此所占内存大小
	char d = 'a';			 //  0			 1
	short b = 2;			 //	 2	         4	
	int a = 20;				 //  4			 8
	long c = 8;				 //  4			12
}x;
struct B{					 //对齐数	至此所占内存大小
	int a = 20;				 //  0			 4
	short b = 2;			 //  2			 6
	long c = 8;				 //  4          12
	char d = 'a';			 //  1 		    16
}y;

We see, just because of the different structure of the order will result in members of the different structure size. That in the design of the structure, we should not only meet the alignment, but also save space, how to do: make a small footprint members as much as possible together.

  • Structure parameter passing time, and shall address structure.

Bit segment size calculated

  1. Bit segment members may be int unsigned int signed int or char (belonging to the family shaping) type.
  2. Member Name bit segment behind a colon and a number, digit number represents the percentage of members change variables.
  3. Sterically section open: if the structure contains only char type members, the structure of 1 byte units in open spaces, or open space of 4 bytes.
struct A
{
	int _a : 2;			
	int _b : 5;
	int _c : 10;
	int _d : 30;
};

int type 4-byte size of open space, the first space is possible to store a four-byte _a, _b, _c, since 2 + 5 + 10 <= 32, _d reopened only 4 bytes, the result is 8 bytes.

struct S
{
	char a : 3;
	char b : 4;
	char c : 5;
	char d : 4;
};

char 1 byte size according to the type of open space, the first space may be able to save one byte 1 a, b, because 3 + 4 <= 8, _c, _d reopened only 2 bytes respectively stored, so the result is 3 bytes.

struct C
{
	char a : 3;
	char b : 4;
	int c : 10;
	int d : 30;
};

The answer is 12, the presence of int, 4 bytes of the structure to open space, a space may be able to save a first and B, two four-byte space required store f c and d, a total of 12 bytes.

struct C
{
	char a : 3;
	char b : 4;
	long long int c : 30;
};

The answer is 16.4 + 8 stores data, the total size of the structure if the integral multiple of the maximum number of aligned, maximum alignment is 8, the result is 16.

Guess you like

Origin blog.csdn.net/Vickers_xiaowei/article/details/89681497