Structure memory alignment

 
 
#include <stdio.h>
#include <iostream>

typedef struct
{
	char a; //offset address 0
	int b; //offset address 4
	char c[3]; //Offset address 8
	double d; //offset address 16
}S;

int main(int argc, char *argv[])
{
	S s1;
	s1.a = 'a';
	s1.b = 0x020304;
	s1.d = -125;
	for (int i = 0; i < 3; i++)
		s1.c[i] = 'A' + i;

	unsigned int s1_addr = (unsigned int)&s1;
	unsigned int a_addr = (unsigned int)&s1.a;
	unsigned int b_addr = (unsigned int)&s1.b;
	unsigned int c_addr = (unsigned int)&s1.c;
	unsigned int d_addr = (unsigned int)&s1.d;

	//std::cout << s1.a << s1.b << s1.c << std::endl;  
	printf("sizeof(S) = %d\n\n", sizeof(S));
	printf("s1.a = %u\n", s1.a);
	printf("s1.b = %d\n", s1.b);
	printf("s1.c = %s\n", s1.c);
	printf("s1.d = %f\n\n", s1.d);

	printf("s1_addr = %lu = 0x%x\n", s1_addr, s1_addr);
	printf("a_addr  = %lu = 0x%x\n", a_addr, a_addr);
	printf("b_addr  = %lu = 0x%x\n", b_addr, b_addr);
	printf("c_addr  = %lu = 0x%x\n", c_addr, c_addr);
	printf("d_addr  = %lu = 0x%x\n\n", d_addr, d_addr);

	printf("s1_addr %% sizeof(maxType) = %lu %% %d = %d\n", &s1, sizeof(double), s1_addr % sizeof(double));

	printf("a_addr  %% sizeof(char)    = %lu %% %d = %d\n", a_addr, sizeof(char), a_addr % sizeof(char));
	printf("b_addr  %% sizeof(int)     = %lu %% %d = %d\n", b_addr, sizeof(int), b_addr % sizeof(int));
	printf("c_addr  %% sizeof(char[])  = %lu %% %d = %d\n", c_addr, sizeof(char), c_addr % sizeof(char));
	printf("d_addr  %% sizeof(double)  = %lu %% %d = %d\n\n", d_addr, sizeof(double), d_addr % sizeof(double));
	system("pause");
	return 0;
}

1. Test environment: win7-64 VS2015 (32-bit)

2. Operation results

(1) The first address of the structure is divisible by 8



(2) The first address of the structure is divisible by 4



3. Analysis

The memory occupied by a structure is related to the declaration order of its members in the structure. The memory alignment rules for its members are as follows:

1. The offset (offset) of each member of the structure relative to the first address of the structure is an integer multiple of the size of the (this) member. If necessary, the compiler will add padding bytes (internaladding) between members;

2. The total size of the structure is an integer multiple of the size of the widest basic type member of the structure. If necessary, the compiler will add padding bytes (trailingpadding) after the last member.

3. Byte alignment depends on the compiler. Each member is according to its own alignment bytes (char=1, int=4, double=8) and PPB ( the specified alignment bytes, defined by the #pragam pack macro, 32-bit machine defaults to 4 ) two The one with the smallest number of bytes is aligned to minimize the length.

4. When determining the widest basic type member of a composite structure including structure variables in the structure member attribute, the sub-members of the composite type member should be included. However, when determining the offset position of a member of a composite type, the composite type is considered as a whole.

5. Summarize a formula: the size of the structure is equal to the offset of the last member plus its size plus the number of padding bytes at the end, namely:

sizeof( struct ) = offsetof( last item ) + sizeof( last item ) +sizeof( trailing padding )

4. Related References

(1) https://www.cnblogs.com/qintangtao/archive/2013/03/06/2945674.html

(2)https://blog.csdn.net/chengonghao/article/details/51861493

(3)https://blog.csdn.net/liujianli123/article/details/47045929

(4)https://blog.csdn.net/u012571728/article/details/53583904?utm_source=itdadao&utm_medium=referral

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325559671&siteId=291194637