Parsing the struct of structural body in C language _c language

Source: Internet
Author: User

First look at the three conceptual values of the structure alignment:

Default alignment values for data types (self-aligning):
1. Basic data type: the length of the base type for the specified platform. In the case of a 32-bit machine, the char alignment value is 1,short to 2,int,float for 4,double 8;
Structure body: The value whose data member is the largest in the default alignment value.
2. Specify alignment Value: Value value of the specified alignment when #pragma pack (value).
3. Valid alignment values for data types: The default alignment value and the value of the specified alignment value.
With these values, we can easily discuss the members of the specific data structures and their own alignment. Valid alignment value n is the final value used to determine how the data is stored in the address, most importantly. A valid alignment of n means "Snap to n", meaning "offset%n=0" for that data. In the data structure, the variables are discharged according to the order of definition. The starting address of the first data variable is the starting address of the structure. The member variable of the struct is to be aligned with the emissions (some bytes must be filled in front of the non aligned member). To ensure that it is in the alignment position, the structure itself should also be based on its own effective alignment of the value of the circle (that is, the overall length of the structure is the structure of the effective alignment of the value of the integer multiple).

With the above analysis, we need to know four values for the structure to be byte-aligned:

    • Specifies the alignment value: The alignment value specified in the code, recorded as Packlen;
    • Default Alignment value: Each data member and structure in the structure itself has a default alignment value, recorded as Defaultlen;
    • Member offset: That is, relative to the length of the starting position of the structure, recorded as offset;
    • Member Length: The length of each data member in the structure (the length after which the member of the structure is padded), recorded as Memberlen.

and two rules:

1. Alignment Rules:
Offset% Vaildlen = 0, where Vaildlen is a valid alignment value Vaildlen = min (Packlen, Defaultlen);

2. Fill rule:
If a member variable does not conform to the alignment rule, it needs to be padded, and a few bytes ahead of it to ensure that the member is aligned. The number of bytes to be populated is Padlen:

Padlen = Getpadlen (offset, defaultlen);
int Getpadlen (int offsetlen, int defaultlen)
{
  int vaildlen = min (packlen,defaultlen);
  if (0 = Vaildlen | | | 0 = offsetlen% vaildlen)
  {return
    0;
  }
  Return Vaildlen-(offsetlen% vaildlen);
}

Structure Alignment algorithm idea: Depth first fill

Align the inner structure first;
Calculating its defaultlen, Memberlen and offset for each data member;

Compute each data member again;
For the basic data type member Defaultlen=memberlen, the Defaultlen is equal to the largest memberlen of all its members for the struct member;
The Memberlen of the member is added to the offsetlen of the current member when traversing.
Apply alignment and padding rules: populate Padlen bytes before the current structure member;


An example is provided:

struct{short

 A;

 Short B;

 Short C; }a; sizeof (A) = 6; 

(VC6 is the same as GCC)

struct{

 long A;

 Short C; }a; sizeof (A) = 8;

(VC6 is the same as GCC), its memory allocation is: A1 A2 A3 A4, C1 C2 x x (A1 is the first byte of a, X is a padded byte, the same below)

struct{

int A;

Char b;

Short C; }a;

sizeof (A) = 8;

A's memory allocation is:

 A1 A2 A3 A4, B1 x C1 c2
struct{

char A;

int b;

Short C; }A1;

sizeof (A1) = 12;

(VC6 is the same as GCC)

The A1 memory allocation is:

A1 x x x, B1 b2 b3 B4, C1 c2 x x

The following are more complex situations where a struct is a member

struct{

int A;

Doubl b;

Short C; }a; sizeof (A) = VC6 (same as GCC)

struct{

  char a,b;

int C;

Double D;

Short E;

struct A h;

} B;

sizeof (B) = +//(same as GCC VC6)

A's memory distribution:

A1 A2 A3 A4 x x x x, B1 b2 b3 b4 b5 b6 b7 B7, C1 c2 x x x x x x

B's Memory distribution:

 
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.