(1) Basic Concepts
The sizeof operator provides the storage size of its operands in bytes. The operand can be an expression or a type name enclosed in parentheses. The storage size of the operands is determined by the type of the operands.
(2) Usage
1. Used for Data Types
Sizeof usage form: sizeof (type), such as sizeof (int)
2. Used for variables
Sizeof format: sizeof (var_name) or sizeof var_name
Variable names can be enclosed in parentheses. For example, sizeof (var_name) and sizeof var_name are all in the correct format. Brackets are more commonly used. Most programmers use this form.
Note: the sizeof operator cannot be used for function types, which are incomplete types or bit fields. An incomplete data type refers to a data type with an unknown storage size, such as an array type with an unknown storage size, an unknown content structure, a union type, and a void type.
For example, if sizeof (max) is defined as int max () and sizeof (char_v), if char_v is defined as char char_v [max] and MAX is unknown, sizeof (void) are not in the correct format.
(3) structure of sizeof applications
See the following structure:
Copy codeThe Code is as follows: struct MyStruct
{
Double doub;
Char ch;
Int I;
};
What will happen if sizeof is used for the structure MyStruct? What is sizeof (MyStruct? You may ask:
Sizeof (MyStruct) = sizeof (double) + sizeof (char) + sizeof (int) = 13
The following is the test code:
Copy codeThe Code is as follows: # include
Using namespace std;
Struct MyStruct
{
Double doub;
Char ch;
Int I;
};
Int main ()
{
MyStruct MS;
Cout <sizeof (MS) <endl;
Return 0;
}
Test results:
But when you test the size of the above structure in the VC, you will find that sizeof (MS) is 16. In fact, this is a special processing of variable storage by VC. To increase the CPU storage speed, VC performs "alignment" on the starting addresses of some variables. By default, VC specifies that the offset of the starting address of each member variable to the starting address of the structure must be a multiple of the bytes occupied by the variable type.
Alignment of common types
| Type |
Alignment (offset of the starting address of the variable to the starting address of the structure) |
| Char |
The offset must be a multiple of sizeof (char), that is, 1. |
| Int |
The offset must be a multiple of sizeof (int), that is, 4. |
| Double |
The offset must be a multiple of sizeof (double), that is, 8. |
| Short |
The offset must be a multiple of sizeof (short), that is, 2. |
| Float |
The offset must be a multiple of sizeof (float), that is, 4. |
When each member variable is stored, the space is requested in sequence based on the order in which the structure appears, and the positions are adjusted according to the alignment above. The vacant byte VC is automatically filled. At the same time, to ensure that the size of the structure is a multiple of the number of byte boundary values of the structure (that is, the number of bytes occupied by the Type occupying the maximum space in the structure, therefore, after applying for space for the last member variable, the vacant bytes will be automatically filled as needed.
Copy codeThe Code is as follows: struct MyStruct
{
Double doub;
Char ch;
Int I;
};
When allocating space for the above structure, VC allocates space for the first member doub according to the sequence and alignment of the member variables, the starting address is the same as the starting address of the structure (the offset 0 is just a multiple of sizeof (double). The member variable occupies eight bytes; next, allocate space for the second member ch. the offset of the next allocable address to the starting address of the structure is 8, which is a multiple of sizeof (char, therefore, the location where ch is stored in the offset of 8 satisfies the alignment. This member variable occupies sizeof (char) = 1 byte. Then, it allocates space for the third member I, in this case, the offset of the next allocable address to the starting address of the structure is 9, not a multiple of sizeof (int) = 4. To meet the offset constraints of alignment, VC automatically fills in three bytes (these three bytes do not have anything). At this time, the next address that can be allocated has a 12 offset to the starting address of the structure, which is exactly sizeof (int) = a multiple of 4, so store I in the place where the offset is 12. The member variable occupies sizeof (int) = 4 bytes. At this time, all member variables in the entire structure are allocated space. The total occupied space is: 8 + 1 + 3 + 4 = 16, which is exactly the number of bytes in the structure (that is, the number of bytes occupied by the Type occupying the maximum space in the structure sizeof (double) = 8) so there is no vacant byte to fill. Therefore, the size of the entire structure is sizeof (MyStruct) = 8 + 1 + 3 + 4 = 16. Among them, three bytes are automatically filled by VC and nothing makes sense.
Next, let's take another example to change the position of the member variable of MyStruct above to the following:
Copy codeThe Code is as follows: struct MyStruct
{
Char ch;
Double doub;
Int I;
};
In the VC environment, we can obtain that sizeof (MyStruct) is 24. Based on the space allocation principles mentioned above, we will analyze how VC allocates space for the above structure:
Copy codeThe Code is as follows: struct MyStruct
{
Char ch; // The offset is 0, which is aligned and occupies 1 byte;
Double doub; // the offset of the next available address is 1, not sizeof (double) = 8
// Multiple. You need to add 7 bytes to make the offset 8 (the alignment is satisfied ).
// Method), so VC automatically fills in 7 bytes, And the doub is stored at an offset of 8
// Address, which occupies 8 bytes.
Int I; // the offset of the next available address is 16, which is twice that of sizeof (int) = 4.
// Number, which meets the alignment of int, so no VC auto-fill is required.
// Put the address at the offset of 16, which occupies 4 bytes.
}; // All member variables are allocated space. The total size of the space is 1 + 7 + 8 + 4 = 20, not a structure.
// Number of knots (that is, the number of bytes occupied by the largest space type in the structure sizeof
// (Double) = 8), so four bytes need to be filled to meet the structure size
// Sizeof (double) = a multiple of 8.
Therefore, the total size of this structure is: sizeof (MyStruct) is 1 + 7 + 8 + 4 + 4 = 24. Among them, 7 + 4 = 11 bytes are automatically filled by VC, and nothing makes sense.
The special processing of the structure storage by VC does increase the speed of the CPU storage variable, but sometimes it also brings some trouble. We also shield the default alignment of the variables, you can set the alignment of variables.
# Pragma pack (n) is provided in VC to set the variable to n-byte alignment. N-byte alignment means the offset of the Start address of the variable. First, if n is greater than or equal to the number of bytes occupied by the variable, the offset must meet the default alignment mode, second, if n is less than the number of bytes occupied by the variable type, the offset is a multiple of n, and the default alignment is not required. The total size of the structure also has a constraint, which is divided into the following two cases: if n is greater than the number of bytes occupied by all member variable types, the total size of the structure must be a multiple of the space occupied by the largest variable; otherwise, it must be a multiple of n.
The following is an example of its usage:
Copy codeThe Code is as follows: # pragma pack (push) // Save the alignment status
# Pragma pack (4) // set to 4-byte alignment
Struct MyStruct
{
Char ch;
Double doub;
Int I;
};
# Pragma pack (pop) // restore alignment
Test results:
(4) sizeof usage Summary
1. the parameter is a data type or a common variable.
For example, sizeof (int) and sizeof (long. In this case, it should be noted that the results of different system systems or compilers may be different. For example, the int type occupies 2 bytes in a 16-bit system and 4 bytes in a 32-bit system.
2. the parameter is an array or pointer.
The following is an example.
Copy codeThe Code is as follows: int a [50]; // sizeof (a) = 4*50 = 200; calculates the space occupied by the array.
Int * a = new int [50]; // sizeof (a) = 4; a is a pointer, and sizeof (a) is the pointer size. In a 32-bit system, of course, it occupies 4 bytes.
3. Other parameters.
Copy codeThe Code is as follows: int func (char s [5])
{
Return 1; // when the function parameter is passed, the system processes it as a pointer, so sizeof (s) is actually the pointer size.
}
Sizeof (func ("1234"); // because the return type of func is int, it is equivalent to sizeof (int), and its value is 4.