#pragma pack (n)

Source: Internet
Author: User

#pragma pack (n) Important rules: 1, each member of a complex type is stored sequentially in memory in the order in which it is declared, the address of the first member is the same as the address of the entire type, and 2, each member is aligned, that is, each member is aligned in its own way and minimizes the length The rule is that each member is aligned by its type's alignment parameter (usually the size of this type) and the specified alignment parameter (which must be an integer multiple of 2 for the occurrence of a single char, such as n=2, when Char occupies one bit, but one is empty; char [2], directly stored, not empty); 3 , a data member of a struct, union, or class, first placed at an offset of 0, followed by the alignment of each data member, according to the value specified by the #pragma pack and the smaller of the data member's own length of two, i.e., when #pragma When the value specified by pack is equal to or exceeds the length of all data members, the size of the specified value will not have any effect; 4, the alignment of a complex type (such as a structure) is based on the smaller value between the largest data member in the struct and the specified value of the #pragma pack. , so that when the member is a complex type, you can minimize the length, 5, the overall length of the structure must take the integer multiple of all the alignment parameters used, not enough to fill the empty bytes, that is, take all the alignment parameters used by the largest value of the integer multiples, because the alignment parameters are 2 N-th square ; This ensures that each item is aligned at the same time as the array is handled, #pragma pack (n) struct node1{int A;char b;short C;char D; Test1;sizeof (test1) n=1, size 8, a (4) +b (1) +c (2) +d (1) n=2, size of ten, A (4) +b (2) +c (2) +d (2) n=4, size, a (4) +b (2) +c (2) +d (2)  struct node2{int A;char b;struct node1 C;char D;} Test2;sizeof (test2) n=1, size 4, a () +b (1) +c (8) +d (1) n=2, size, a (4) +b (2) +c (Ten) +d (2) n=4, size A (4) +b (2) +c (+d) (2)   struct Node3{int A;char b[2];struct node1 C;} Test3;sizeof (TEST3) n=1, size 4, a () +b (2) +c (8) n=2, size, a (4) +b (2) +c (n=4), size A (4) +b (2) +c (&NB)Sp;union Un1{char a[5];int i;} U1;sizeof (U1) n=1, size 5, a (5) n=2, size 6, a (5) n=4, size 8 A (5)  struct node4{int A;char b[3];union un1 c;} Test4;sizeof (test4) n=1, size 4, a () +b (3) +c (5) n=2, size, a (4) +b (4) +c (6) n=4, size A (4) +b (4) +c (8)
#include <iostream>using namespacestd;#pragmaPack (4)structnode1{intA; Charb;  ShortC; CharD;} Test1;structnode2{intA; Charb; structNode1 C; CharD;} Test2;structnode3{intA; Charb[2]; structNode1 C;} Test3;union un1{Chara[5]; inti;} U1;structnode4{intA; Charb[3]; Union UN1 c;} test4;intMain () {cout<<"int"<<sizeof(int) <<Endl; cout<<" Short"<<sizeof( Short) <<Endl; cout<<"Char"<<sizeof(Char) <<Endl; cout<<"Long"<<sizeof(Long) <<Endl; cout<<"Double"<<sizeof(Double) <<Endl; cout<<"test1"<<sizeof(test1) <<Endl; cout<<"test2"<<sizeof(test2) <<Endl; cout<<"test3"<<sizeof(TEST3) <<Endl; cout<<"U1"<<sizeof(U1) <<Endl; cout<<"test4"<<sizeof(TEST4) <<Endl; System ("Pause"); return 0;}

#pragma pack (n)

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.