C + + notes __c++

Source: Internet
Author: User

Let's first introduce a related concept--the offset.
An offset is the difference between the address of a member in a struct variable and the address of a struct variable.
The structure body size equals the offset of the last member plus the size of the last member.
Obviously, the address of the first member of the struct variable is the first address of the struct variable. So
struct STU1

{

int i;

char c;

Int J;

The first member I has an offset of 0. -Dead.

The offset of the second member C is the offset of the first member plus the size of the first member (0+4) with a value of 4;
The offset of the third member J is the offset of the second member plus the size of the second member (4+1) with a value of 5.

In fact, the compiler follows two principles when compiling a program because of the need to address alignment when storing variables:
The offset of a member in a struct variable must be an integer multiple of the member size (0 is considered to be an integer multiple of any number)
The structure size must be an integer multiple of the size of all members.

One, what is the byte alignment
A variable of a basic type occupies n bytes in memory, the starting address of the variable must be divisible by n, that is, to hold the starting address% n = 0, then the variable is byte-aligned, and for the struct and the consortium, this n takes the largest number of bytes in the members of all its base types;
Memory space is divided in bytes as the base unit, and theoretically, it seems that access to any type of variable can start at any address, but the reality is that when accessing a particular type of variable, it often starts at a particular memory address. This requires that all types of data be arranged in space according to certain rules, instead of one after the other in order; The reason is that in order to make the CPU of different architectures increase the speed of accessing memory, it is stipulated that the data of certain type can be accessed only from a specific memory location; All types of data can only be discharged in memory space according to the corresponding rules, but cannot be discharged sequentially, continuously and one by one; this is the memory alignment;

For char data, its own alignment value is 1, for the short type 2, for the int,float,double type, its own alignment value is 4, Unit bytes.
There are four conceptual values in this:
1. The alignment value of the data type itself: is the value of its own alignment for the underlying data type as explained above.
2. Specify alignment Value: Value value of the specified alignment when #progma Pack (value).
3. A struct or a class's own alignment value: The value of its member's own alignment value.
4. Valid alignment values for data members, structs, and classes: their own alignment values and the value of the specified alignment value.

How exactly does the structure align? Some people have summed up the principle of alignment, specifically where to see now has not been remembered, here reference to the experience of predecessors (in the absence of #pragma pack macros):
Principle 1, data member alignment rules: A data member of a struct (struct or union Union), where the first data member is placed at 0, and the starting position of each data member store starts at an integer multiple of that member's size (for example, int is 4 bytes in 32-bit), from 4 Integer multiple addresses to begin storage).

Principle 2, struct body as member: If a struct has some members of a struct, the member of the struct body is to be stored from the integer multiple address of its internal maximum element size. (struct A contains elements such as char,int,double in the struct b,b, which should be stored from the integer of 8.) ) principle

3, finishing work: The total size of the structure, that is, the result of sizeof, it must be the largest member of the whole number of times, insufficient to be filled.

Copy Constructors
Friend Yuan

Constructors with multiple inheritance and inline objects

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.