Data Alignment of struct # pragma discussion,

Source: Internet
Author: User

Data Alignment of struct # pragma discussion,

If someone used a structure or a consortium to ask me how many bytes of memory this structure occupies, I must think this person is a bit low until an intern recruitment simulation question from a certain company appears, I had to review this issue,

The problem is roughly as follows:

typedef struct _A{  char a;  int b;  float c;  double d;  int *pa;  char* pc;  short e;}A;#pragma pack(pop)int main(int argc, char *argv[]){  printf("size = %d\n",sizeof(A));  return 0;}

The program output result is ().
A size = 48
B size = 44
C size = 40
D size = 36

At first glance, isn't this simple? 1 + 4 + 4 + 8 + 4 + 4 + 2 = 27 bytes. The minimum answer is 36!

Some people asked for Data Alignment, so they noticed the pre-compiled command # pragma pack (pop), one hundred degrees. The answer is as follows:

Purpose: Specify the structure, union, and packing alignment of class members;

Syntax: # pragma pack ([show] | [push | pop] [, identifier], n)

Note:

1. pack provides data Declaration-level control, which does not work for definition;

2. No parameter is specified when pack is called. n is set to the default value;

3. Once the data type is changed, the direct effect is to reduce memory usage, but the performance will decrease;

Syntax analysis:

1. show: an optional parameter. The number of bytes of the current packing aligment is displayed as a warning message;

2. push: an optional parameter. the current packing alignment value is used for the stack operation. the stack here is the internal compiler stack, and the current packing alignment is set to n; if n is not specified, the current packing alignment value is pushed to the stack;

3. pop: an optional parameter. The top record is deleted from the internal compiler stack. If n is not specified, the top record of the current stack is the new packing alignment value. If n is specified, then n will be the new packing aligment value. If identifier is specified, the record in the internal compiler stack will be pop until the identifier is found, and then the identitier is pop, set the value of packing alignment to the record at the top of the current stack. If the specified identifier does not exist in the internal compiler stack, the pop operation is ignored;

4. identifier: an optional parameter. When used together with push, a name is assigned to the record currently pushed into the stack. When used together with pop, pop all records from the internal compiler stack until the identifier is pop. If the identifier is not found, ignore the pop operation. 5, n: optional parameter; specify the value of packing, in bytes. The default value is 8. Valid values are 1, 2, 4, 8, and 16.

The internal compiler stack is empty because there is no push operation. Therefore, this pre-compilation setting does not work here. Use the following command to view the current packing alignment = 8, is the default alignment parameter.

#pragma pack(show)

Let's take a look at the data alignment rules. Complex Data Alignment mainly includes the alignment of data members and the overall alignment of complex data. Both of them can be summarized

"Select a small alignment from the alignment parameter and the specified alignment parameter"

The alignment parameter of a data member is the type that occupies the length of bytes. The alignment parameter of complex data is the alignment parameter of the largest data member, the offset is equal to offset % N = 0, and the data length is as short as possible. The offset of the first data member is 0,

Therefore, we can analyze

Alignment of member variables

The offset of a is 0, which occupies one byte.

At this time, the offset is 0 + 1 = 1, B's own alignment parameter is 4, packing alignment = 8, Gu Ying alignment is 4, Gu his offset is 4, (1 + 3) % 4 = 0

At this time, the offset is 4 + 4 = 8, and c should be aligned with 4, 8% 4 = 0, so the offset is 8

At this time, the offset is 8 + 4 = 12, and the length of d is 8, and the specified alignment parameter is 8. Therefore, the alignment parameter is 8, so the offset should be 12 + 4 = 16, because 16% 8 = 0

At this time, the offset is 16 + 8 = 24, while the alignment parameter of pa is 4, 24% 4 = 0, so the offset is 24.

At this time, the offset is 24 + 4 = 28, and the pc offset is also 28.

The offset is 24 + 4 = 32, and the alignment parameter of e is 2, 32% 2 = 0. Therefore, the offset is 32 and the length is 2. After alignment of data members, the size of structure A is

32 + 2 = 34 bytes

Overall alignment

In struct A, the maximum member variable length is 8, and the specified packing alignment is 8, so it should be aligned with 8, and the size of the data member after alignment is 34, after the overall alignment, the size should be 40 because 40% 8 = 0.

 

 

Related Article

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.