About character alignment

Source: Internet
Author: User

A friend posted the following code:
# Pragma pack (4)
Class testb
{
Public:
Int AA;
Char;
Short B;
Char C;
};
Int nsize = sizeof (testb );
Here, the nsize result is 12, which is expected.

The following code removes the first member variable:
# 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.

Explanation
# Pragma pack (4)
Class testb
{
Public:
Int AA; // The first member, which is placed at the [] offset,
Char A; // The second member. Its length is 1, # pragma pack (4), with a small value, that is, 1. Therefore, the Member is aligned in one byte, at the offset [4.
Short B; // The Third Member, whose length is 2, # pragma pack (4), takes 2, and is aligned by 2 bytes, so it is placed at the offset [6, 7.
Char C; // fourth, with a length of 1 and placed at the position of [8.
};
The actual memory occupied by this class is 9 bytes.
The alignment between classes is based on the maximum member length within the class and a smaller alignment among the values specified by # pragma pack.
In this example, the alignment length between classes is Min (sizeof (INT), 4), that is, 4.
9 The result of 4-byte rounding is 12, so sizeof (testb) is 12.


 


If
# Pragma pack (2)
Class testb
{
Public:
Int AA; // The first member, which is placed at the [] offset,
Char A; // The second member. Its length is 1, # pragma pack (4), with a small value, that is, 1. Therefore, the Member is aligned in one byte, at the offset [4.
Short B; // The Third Member, whose length is 2, # pragma pack (4), takes 2, and is aligned by 2 bytes, so it is placed at the offset [6, 7.
Char C; // fourth, with a length of 1 and placed at the position of [8.
};
// It can be seen that the above position is completely unchanged, but the class is changed to 2-byte alignment, and 9 is rounded to 2. The result is 10.
// Therefore, sizeof (testb) is 10.

Finally, check the original post:
The following code removes the first member variable:
# 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.

 

 

# Pragma preprocessing instructions
Among all the pre-processing commands, the # pragma command may be the most complex. It is used to set the compiler status or to instruct the compiler to complete some specific actions. # The Pragma command provides a method for each compiler to provide the unique features of the host or operating system while maintaining full compatibility with C and C ++ languages. According to the definition, the compilation instructions are proprietary to machines or operating systems and are different for each compiler.
The format is generally: # pragma para
Here, para is a parameter. Below are some common parameters.

(1)Message parameter. The message parameter is one of my favorite parameters. It can be used in the compilation information output window.
Output the corresponding information, which is very important for controlling the source code information. The usage is as follows:
# Pragma message ("message text ")
When the compiler encounters this instruction, it prints the message text in the compilation output window.
When we define many Macros in the program to control the source code version, we may forget whether these macros are correctly set, in this case, we can use this command to check it during compilation. Suppose we want to determine whether we have defined the _ x86 macro in the source code. The following method can be used:
# Ifdef _ x86
# Pragma message ("_ x86 macro activated !")
# Endif
After we define the _ x86 macro, the application will display "_
X86 macro activated !". We won't be scratching our heads because we don't remember some specific macros we defined.

(2)The other Pragma parameter that is used more frequently is code_seg. Format:
# Pragma code_seg (["section-name" [, "section-class"])
It can set the code segment where function code is stored in the program. It is used when we develop the driver.
(3)# Pragma once (commonly used)
You only need to add this command at the beginning of the header file to ensure that the header file is compiled once. This command is actually available in vc6, but it is not widely used in consideration of compatibility.

(4)# Pragma hdrstop indicates that the header file is pre-compiled till now, and the header file is not pre-compiled. BCB can pre-compile the header file to speed up the link, but if all header files are pre-compiled, it may occupy too much disk space. Therefore, this option is used to exclude some header files.

Sometimes there is a dependency between units. For example, unit a depends on unit B. Therefore, Unit B must be compiled before unit. You can use # pragma startup to specify the compilation priority. If # pragma package (smart_init) is used, BCB will be compiled based on the priority.

(5)# Pragma resource "*. DFM" indicates adding resources in the *. DFM file to the project. * DFM includes the form
The definition of the appearance.

(6)# Pragma warning (Disable: 4507 34; once: 4385; error: 164) is equivalent:
# Pragma warning (Disable: 4507 34) // do not display the 4507 and 34 Warnings
# Pragma warning (once: 4385) // only one warning message is reported once
# Pragma warning (error: 164) // the error message 164 is used as an error. This pragma warning also supports the following formats:
# Pragma warning (push [, N])
# Pragma warning (POP)
Here N represents a warning level (1---4 ).
# Pragma warning (push) saves the existing warning status of all warning information.
# Pragma warning (push, n) saves the existing warning status of all warning information and sets the global warning level to n.
# Pragma warning (POP) pops up the last warning message to the stack.
All changes are canceled. For example:
# Pragma warning (push)
# Pragma warning (Disable: 4705)
# Pragma warning (Disable: 4706)
# Pragma warning (Disable: 4707)
//.......
# Pragma warning (POP)
At the end of the Code, save all warning information (including 4707, and ).
(7) Pragma comment (...)
This command puts a comment record into an object file or executable file.
Common lib keywords can help us to connect to a library file.

Each compilation program can use the # pragma command to activate or terminate some compilation functions supported by the Compilation Program. For example, loop optimization:
# Pragma loop_opt (on) // activate
# Pragma loop_opt (off) // terminate
Sometimes, some functions in the program will make the compiler send a warning that you are familiar with and want to ignore, such as "parameter XXX is never used in function XXX". You can do this:
# Pragma warn-100 // turn off the warning message for warning #100
Int insert_record (REC * r)
{/* Function body */}
# Pragma warn + 100 // turn the warning message for warning #100 back on
The function generates a warning message with a unique signature 100, so that the warning can be temporarily terminated.
Each compiler has different implementations for # pragma, and the effectiveness in one compiler is almost ineffective in other compilers. You can view it in the compiler documentation.

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.