About structure size calculation in C language

Source: Internet
Author: User

The calculation of the size of the structure. On the internet, a lot of things are different. Alignment misalignment, offset, etc... Here is a brief summary of the following examples: Alignment Principle: The end offset of each member must be aligned to a multiple of the latter member typeThe principle of completing: Final size is a multiple of the maximum number of members take a question to do an example: "(start offset) + here byte = end offset】
1 structMyStruct2 {3     intI//(0) + 4 = 4, followed by 2, aligned4     CharC//(4) + 2 = 6, followed by a struct, directly open to see the first, int=4, so to arranges (4) + 4 = 85     structinnerstruct6     {7         intI//(8) + 4 = 12, next 4, already aligned (Learn Java note, 32-bit C language Long is 4, and Java is not the same)8         LongL//(12) + 4 = 16, next 8, already aligned9         DoubleD//(16) + 8 = 24, next 1, already alignedTen         CharC//(24) + 1 = 25, which is the last one, aligns it to a multiple of the maximum value of 8, so the completion is (24) + 8 = 32, the final result =32 One } innerstruct; A }; 
"Note" If Specify the justification byte, this designation is compared to the next one when aligned, and the smaller is aligned. At the time of completion, compared with the largest, also take the smaller one to complement. The code is as follows:
1 structA {2     Charb;3     intA;4      ShortC;5 };6 7 #pragmaPack (2)/* Specify 2-byte alignment */8 structB {9     Charb;Ten     intA; One      ShortC; A }; - #pragmaPack ()/* Cancels the specified alignment, restores the default alignment */ -  the  -  - #pragmaPack (1)/* Specify 1-byte alignment */ - structC { +     Charb; -     intA; +      ShortC; A }; at #pragmaPack ()/* Cancels the specified alignment, restores the default alignment */ -  - //calculate the occupied bytes -     ints1=sizeof(structA); -     intS2=sizeof(structB); -     ints3=sizeof(structC); in      -printf"%d\n", S1); toprintf"%d\n", S2); +printf"%d\n", S3);
Output: 12//By the next alignment, by the maximum 8//by 2 for alignment and completion of 7//by 1 to align and complement, the equivalent of directly calculate each size and then add

About structure size calculation in C language

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.