Sizeof alignment with memory

Source: Internet
Author: User

Sizeof is an operator in C/C ++. Simply put, the function is to return the memory bytes occupied by an object or type. The returned type is size_t.

 

Syntax:
Sizeof has three syntax forms:
1) sizeof (object); // sizeof (object );
2) sizeof (type_name); // sizeof (type );

3) sizeof object; // sizeof object;

Therefore:

Int I;
Sizeof (I); // OK
Sizeof I; // OK
Sizeof (INT); // OK
Sizeof int; // Error

 

Sizeof can be used to evaluate an expression. The compiler determines the size based on the final result type of the expression, and generally does not calculate the expression.

Char fun () <br/>{< br/> cout <"Fun ()" <Endl; <br/> return 'C '; <br/>}</P> <p> void main () <br/>{< br/> cout <sizeof (fun () <Endl; // output 1, fun () returns char type, but does not print "Fun ()", that is, fun is not executed <br/> cout <sizeof (2) <Endl; // output 2.0 is an integer <br/> cout <sizeof (2.0 + 3) <Endl; // output 8, is a double, 3 is promoted to integer <br/> cout <sizeof ("Grubby") <Endl; // output 7, the string "Grubby" is not considered const char * <br/>}

According to the c99 standard, functions, expressions of undetermined types, and bit-field members cannot be computed with sizeof values. That is, the following statements are incorrect:

Sizeof (void );

Sizeof (FOO );

Void foo2 (){}
Sizeof (foo2 ());

Struct s
{
Unsigned int F1: 1;
Unsigned int F2: 5;
Unsigned int F3: 12;
};
Sizeof (S. F1 );

 

The computer composition principle teaches us how to speed up the computer's data acquisition speed, otherwise it will take more time to run the command cycle.

For this reason, the compiler will process the struct by default (in fact, the data variables in other places are also the same), so that the basic data type with a width of 2 (short, etc) are located on the address that can be divisible by 2, so that the basic data type (INT, etc.) with the width of 4 is located on the address that can be divisible by 4. In this way, the two numbers may need to be filled with bytes, so the sizeof value of the entire struct increases.

The details of byte alignment are related to compiler implementation, but generally three criteria are met:
1) The first address of the struct variable can be divisible by the size of its widest basic type member;
2) The offset (offset) of each member of the struct to the first address of the struct is an integer multiple of the member size. If necessary, the compiler will add the internal adding between the members );
3) the total size of the struct is an integer multiple of the size of the widest basic type of the struct. If necessary, the compiler will add the trailing padding after the last member ).

 

The basic type refers to the built-in data types mentioned above, such as char, short, Int, float, and double. The "data width" here refers to the size of its sizeof. Because the struct member can be a composite type, for example, another struct, when looking for the widest basic type member, it should include the Child Member of the composite type member, instead of seeing a composite member as a whole. However, when determining the offset of a composite member, the composite type is regarded as a whole.

For example

Struct S1 <br/>{< br/> int I; <br/> char C; <br/> }; <br/> struct S2 <br/>{< br/> char C1; <br/> S1 s; <br/> char C2; <br/> }; <br/> void main () <br/> {<br/> cout <sizeof (S1) <Endl; <br/> cout <sizeof (S2) <Endl; <br/>}

The type of the widest and simplest member of S1 is int. S3 splits S1 when considering the widest and simplest type member, so the widest and simplest type of S3 is int. In this way, the first address of the bucket needs to be divisible by four based on the variables defined by S3, and the entire sizeof (S3) value should also be divisible by four.

The offset of C1 is 0. What is the offset of S? At this time, S is a whole. As a struct variable, it also meets the first three rules. Therefore, the size is 8, the offset is 4, and three Padding Bytes are required between C1 and S, however, there is no need between C2 and S. Therefore, the offset of C2 is 12, and the size of C2 is 13, and 13 cannot be fully divided by 4. In this way, three Padding Bytes must be added at the end. Finally, the value of sizeof (S3) is 16.

 

# Pragma pack (4)
Class testc
{
Public:
Char;
Short B;
Char C;
};
Int nsize = sizeof (testc );
According to the normal filling method, the nsize result should be 8. Why does the result show that nsize is 6?

In fact, many people have incorrect understanding of # pragma pack.
# The alignment length specified by the Pragma pack. The actual use rules are as follows:
Structure, union, or data member of the class. The first one is placed in a place with the offset of 0, and the alignment of each data member is later, follow the value specified by # pragma pack and the smaller value in the length of the data member. That is to say, when the value of # pragma pack is equal to or greater than the length of all data members, the size of this value will not produce any effect.
The overall alignment of the structure is performed based on the smaller value between the largest data member in the structure and the value specified by # pragma pack.

# Pragma pack (4)
Class testc
{
Public:
Char A; // The first member, which is placed at the [0] offset,
Short B; // The second member. Its length is 2, # pragma pack (4), take 2, and align it in 2 bytes, so it is placed at the offset [2, 3.
Char C; // The third, with a length of 1 and placed in the position of [4.
};
The size of the entire class is 5 bytes, alignment by min (sizeof (short), 4) bytes, that is, 2 bytes alignment, and the result is 6
Therefore, sizeof (testc) is 6.

 

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.