byte alignment issues in C + +

Source: Internet
Author: User

Three basic principles


In 1.struct or union or class, the alignment start bit of each member must be an integer multiple of the member's own size;

2. The size of each structure must be an integer multiple of the largest member of the structure, (the second way)

3. The starting bit of struct B within struct a must be an integer multiple of the largest member in B.


Example one:

typedef struct BB {
int id; [0] .... [3]
Double weight; [8] ..... [15] Principle 1
float height; [16]. [19], the total length to 8 of the integer times, padded [20] ... [23] Principle 3
} ;

The result is: 24

Example two:

typedef struct aa{
Char name[2]; [0],[1]
int id; [4] ... [7] Principle 1
Char Score[8]; [8] .... [15]
Short grade; [16],[17]
BB b; [24] ... [47] Principle 2 (the largest member of BB is 8 bits.) So starting from the starting bit from 24)
int A; [48] ... [51] The total length of a multiple of 8 is padded [52] ... [55]
} ;

The result is: 56

Reference: http://blog.csdn.net/hairetz/article/details/4084088


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

byte alignment issues in C + +

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.