Primer zero-based structure byte alignment body

First, byte alignment rules:

1, is generally provided for alignment 1,2,4 byte alignment. Structure must first address the widest type integer multiple addresses within the structure; In addition, each member of the structure of the starting address must be a multiple of the size of their type (special attention needs to be such that the windows, but linux the gcc compiler up to 4-byte alignment), or after a previous fill type 0; herein is an array of special mention must pay attention to, and in some programming techniques, we can use an array of force to achieve byte alignment the goal of. This network programming is very common.

Example: CHAR type such as 1-byte space, its starting position must be divisible. INT is 4 bytes, which is to be the starting position 4 led, and so on. (We assume the starting position of a class or structure to the 0 position, in fact, the compiler is in the open space, look for the starting position can be used as a start address within the structure of the widest type divisible address, so we can assume that it is 0 value, since this value is 0 can be of any type divisible.)

2, the overall size of the structure must be divisible by the alignment value, the default 4 (size of the structure type is less than the default 4).

3, the overall size of the structure must be divisible by the broadest types within this structure. (In fact, on one and the same, but here independent, plays the role of attention, such as in the structure there DOUBLE, so the final size of the structure must be divisible by 8)

Note: GCC is not the case, that could only divisible by four. This is a 32-bit system, the system will use 64 8 divisible manner. Otherwise (2,3 bar), the compiler will add the charge at the end of a certain structure to a specific character filled.

struct T 
{ 
char ch; 
double d ; 
};

 

This result emphasizes the system is 32 is 16 bytes in the VC, GCC for 12 bytes. 64 is still 16 bytes

4, the nested structure for the situation in vivo structure, provision must be defined according to the basic data types, and can not do for the size of the nested structure used in three reference. (Summary) structure can be processed into a char type.

Second, for example:

struct A 
{ 
    int a;    
    char b; 
    short c; 
};  //8
struct B 
{ 
    char b; 
    int a; 
    short c; 
};  // 12
struct C 
{ 
    double t; 
    char b; 
    int a; 
    short c; 
}; //24
struct D 
{ 
    char b; 
    double t; 
    int a; 
    short c; 

};//24

 

In the VC, The SIZEOF four structures, respectively: 8,12,24,24;

We start with the first one (note, when considering the size of the structure, we can ignore fundamental issues starting address, because this Code

Translation will do for us automatically, see description above), is first of all structures in the body of a 4-byte INT, the start address is assumed to be 0, divisible by 4, which is less than or equal to 4 byte alignment and default 0 4 ( INT footprint) is an integer multiple, so that total of four bytes; followed by an integer multiple of the start address is 5, the space of 1 byte CHAR, 4 and 5 is less than 1 (CHAR space) of , it occupies one byte, then a start address is 2 bytes in 5 SHORT, which is less than 4, but not 2 5 bits, so a padded bytes starting from byte 6 , accounting for 2-byte space. Therefore, it occupies a total of 4 + 1 + 1 (s) + 2 = 8; 8/4 = 2; divisible, so that the space occupied by 8 bytes.

Second talk, CHAR without explanation, a byte space occupied and address 0 may be divisible. The INT accounted for 4 bytes of space, so it must be filled in after CHAR 3 bytes, the fourth byte is the real address of INT. SHORT Needless to say, it occupies a total: 1 + 3 (s) + 2 + 4 = 10 bytes, but not divisible by 4 10, so last 2 bytes filled in the structure. Therefore, the actual possession of 10 + 2 = 12 bytes.

On the third, C structures just before the B structure plus a DOUBLE, other are the same, it is supposed to be 20 bytes ah, but we note that Article 3 of the above rules. Must be an integer multiple of the widest type, must distinguish, so to obtain padded 24, D the structure is similar to not speak.

 

Linux system is 32-bit and 64-bit basic data types GCC compiler length table
GCC 32位
sizeof(char) = 1
sizeof(double) = 8
sizeof(float) = 4
sizeof(int) = 4
sizeof(short) = 2
sizeof(long) = 4
sizeof(long long) = 8
sizeof(long double) = 12
sizeof(complex long double) = 24
指针是4字节;
GCC 64位
sizeof(char) = 1
sizeof(double) = 8
sizeof(float) = 4
sizeof(int) = 4
sizeof(short) = 2
sizeof(long) = 8
sizeof(long long) = 8
sizeof(long double) = 16
sizeof(complex long double) = 32

8-byte pointers are

Referring to the example:

GNU C __attribute__ extension mechanism is used to set a function, variable, type attribute, which is used more byte-aligned process problems.
__attribute__ syntax for:
__attribute__ ((syntax list)) the aligned parameter (number) [number of bytes of the minimum alignment] a is used more.
Another parameter is packed indicating "minimum alignment" mode, i.e., the variable byte-aligned, for the field is bit aligned.
#include
struct A{
 char a;          //1Byte
 int b;           //4B
 unsigned short c;//2B
 long d;          //4B
 unsigned long long e; //8B
 char f;               //1B
};
 
struct B{
 char a;
 int b;
 unsigned short c;
 long d;
 unsigned long long e;
 char f;
}__attribute__((aligned));
 
struct C{
 char a;
 int b;
 unsigned short c;
 long d;
 unsigned long long e;
 char f;
}__attribute__((aligned(1)));
 
 
struct D{
 char a;
 int b;
 unsigned short c;
 long d;
 unsigned long long e;
 char f;
}__attribute__((aligned(4)));
 
struct E{
 char a;
 int b;
 unsigned short c;
 long d;
 unsigned long long e;
 char f;
}__attribute__((aligned(8)));
 
struct F{
 char a;
 int b;
 unsigned short c;
 long d;
 unsigned long long e;
 char f;
}__attribute__((packed));
 
struct H{
 char a;
 double b;
};
int main(int argc, char **argv)
{
 struct A a;
 struct B b;
 struct C c;
 struct D d;
 struct E e;
 struct F f;
 
 printf("A = %d, B = %d, C = %d, D = %d, E = %d, F = %d, H = %d\n",
   sizeof(struct A), sizeof(struct B), sizeof(struct C), sizeof(struct D), sizeof(struct E),      sizeof(struct F),sizeof(struct H));
 return 0;
}
 

 

result:
$ ./aligned32 
A = 28, B = 32, C = 28, D = 28, E = 32, F = 20, H = 12 
$ ./aligned64 
A = 40, B = 48, C = 40, D = 40, E = 40, F = 24, H = 16 

C language to share some relevant information

Structure popularization and application
http://www.makeru.com.cn/live/5413_1909.html?s=45051
C language list Fun
http://www.makeru.com.cn/live/1392_338.html?s = 45051

Essential Linux commands and the C programming language
http://www.makeru.com.cn/video/1862.html?s=45051

Release potential: learning to enhance the efficiency, capacity building program
http://www.makeru.com.cn/live/3507_1276.html?s=45051

Guess you like

Origin www.cnblogs.com/923327iu/p/11848655.html