#Pragma Pack (n) and memory allocation

Source: Internet
Author: User
Tags pack reserved

#pragma pack (n)

Explanation One:

Compilers on each particular platform have their own default "alignment coefficients" (also known as Zimo numbers). Programmers can change this coefficient by precompiling the command #pragma pack (n), where n is the "alignment factor" you want to specify.

Rules:

1. Data member Alignment rules: Structure (struct) (or union), the first data member is placed at offset 0, and each subsequent alignment of the data member is performed in the smaller of the value specified by the #pragma pack and the length of the data member itself.

2, the overall alignment rule of a struct (or union): After the data member has been aligned, the structure (or union) itself is aligned, and alignment is performed in the smaller of the value and structure (or union) of the maximum data member length specified by the #pragma pack.

EXPLANATION Two:

N-byte alignment VC special handling of the storage of the structure does improve the speed of the CPU storage variable, but sometimes it also brings some trouble, we also block out the default alignment of variables, we can set the alignment of the variable. VC provides the #pragma pack (n) to set the variable in n-byte alignment. N-byte alignment means that the offset of the starting address of the variable is stored in two ways:

First, if n is greater than or equal to the number of bytes occupied by the variable, the offset must meet the default alignment.

Second, if n is less than the number of bytes occupied by the type of the variable, the offset is a multiple of n, and the default alignment is not met. The total size of the structure also has a constraint, in the following two cases: if n is greater than the number of bytes occupied by all member variable types, the total size of the structure must be a multiple of the space occupied by the most space-consuming variable, otherwise it must be a multiple of n.

The following examples illustrate its use. #pragma pack (push)//Save alignment status

#pragma pack (4)//set to 4-byte alignment

struct Test {char M1 double M4 int m3;}; #pragma pack (POP)//recovery alignment state above the size of the structure body is 16:

The following analysis of its storage, first for the M1 allocated space, its offset is 0, to meet our own set alignment (4-byte alignment), m1 size of 1 bytes. It then starts allocating space for the M4, with an offset of 1, which needs to complement 3 bytes, so that the offset is a multiple of n=4 (because sizeof (double) is greater than 4), and M4 occupies 8 bytes. The space is then allocated for the M3, with an offset of 12, a multiple of 4, and a M3 of 4 bytes. Space has been allocated for all member variables and 16 bytes have been allocated, which satisfies a multiple of n. If the above #pragma pack (4) is changed to #pragma pack (8), then we can get the size of the structure to be 24.

We read these words to describe the head also must be numb, I insist on reading, and then wrote a program myself:

#pragma packs (4)

struct node{

int e;
Char F;
short int A;
Char b;

};

struct node n;

printf ("%d\n", sizeof (n));

I calculated the result is 16, the result of the actual result is:

Then the internal data member of the structure changes position:

#pragma packs (4)

struct node{

Char F;
int e;
short int A;
char b;};

struct node n;

printf ("%d\n", sizeof (n));

Force the alignment of the digits to 2

#pragma packs (2)

struct node{

Char F;
int e;
short int A;
char b;};

struct node n;

printf ("%d\n", sizeof (n));

Force the alignment of the digits to 1

#pragma packs (1)

struct node{

Char F;
int e;
short int A;
char b;};

struct node n;

printf ("%d\n", sizeof (n));

Look at the output and text description is a little dizzy, the following simple to say my decision rules:

In fact, there is the memory byte alignment mechanism, is to minimize the number of memory read. We know that the CPU reads faster than the memory reading speed of at least one order of magnitude, so to save the time spent on computing, only to sacrifice space in exchange for time.

The following example shows how to minimize the number of reads.

#pragma packs (1)

struct node{

Char F;
int e;
short int A;
char b;};

struct node n;

printf ("%d\n", sizeof (n));

This forces the alignment by 1 bytes, it can be understood that all content is read in 1 bytes (for the moment, because it's a good way to understand the memory mechanism), all the other data members are 1-byte integers, so there's no memory for it, Each member is arranged in the actual order in memory, the actual length of the structure is 8

#pragma packs (2)

struct node{

Char F;
int e;
short int A;
char b;};

struct node n;

printf ("%d\n", sizeof (n));

This forces the alignment by 2 bytes. If the memory distribution is still continuous, then int e has to be three times to read to the CPU, so in order to "pay attention to" int e read, so after Char F reserved 1BYTE, the last char B, so the length is 10

#pragma packs (4)

struct node{

Char F;
int e;
short int A;
char b;};

struct node n;

printf ("%d\n", sizeof (n));

This forces the alignment by 4 bytes. So char f is reserved for 3BYTE, and short int A and char B can be read to the CPU at one time (read in 4 bytes), so the length is 12

If n in the #pramga pack (n) is greater than the number of bytes occupied by any member of the struct body, the n value is invalid. The compiler selects the number of bytes in the structure body that is the largest data member as a benchmark for its

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.