Sizeof
This article consists of two parts. The first part focuses on how to use sizeof in VC to find the structure size and problems that are prone to, and provides solutions to the problems, the second part summarizes the main usage of sizeof in VC.
1. Structure of sizeof applications
See the following structure:
Struct mystruct
{
Double D1;
Char D2;
Int in
};
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
However, when we test the above structure in VC, we will find that sizeof (mystruct) 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. Common alignment types are listed below (vc6.0, 32-bit system ).
Type alignment (offset of the starting address of the variable to the starting address of the structure)
The Char offset must be sizeof (char), that is, a multiple of 1.
The Int offset must be a multiple of sizeof (INT), that is, 4.
The float offset must be a multiple of sizeof (float), that is, 4.
The double offset must be sizeof (double), that is, a multiple of 8.
The short offset must be a multiple of sizeof (short), that is, 2.
The long offset must be a multiple of sizeof (long), 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.
The following uses the previous example to illustrate how VC stores the structure.
Struct mystruct
{
Double D1;
Char D2;
Int in;
};
When allocating space for the above structure, VC allocates space for the first member D1 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 D1. the offset of the next allocable address to the starting address of the structure is 8, which is a multiple of sizeof (char, therefore, d1 is stored in an alignment where the offset is 8. This member variable occupies sizeof (char) = 1 byte. Next, it allocates space for the third member in, 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 3 bytes (these three bytes do not have anything), then the offset of the next allocable address to the starting address of the structure is
12, which is just a multiple of sizeof (INT) = 4. Therefore, the type is stored in the place with the offset of 12. This member variable occupies sizeof (INT) = 4 bytes; at this time, the member variables of the entire structure have been allocated space. The total occupied space is 8 + 1 + 3 + 4 = 16, it is just a multiple of the number of byte boundary values of the structure (that is, the number of bytes occupied by the largest space type in the structure sizeof (double) = 8). Therefore, no vacant bytes need to be filled. 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:
Struct mystruct {char DDA; double dda1; int type ;};
How much space does this structure occupy? In the vc6.0 environment, we can obtain that sizeof (mystruc) is 24. Based on the space allocation principles mentioned above, we will analyze how VC allocates space for the above structure. (Simple description)
Struct mystruct {char DDA;
Double dda1;
Int type;
};
All member variables are allocated space. The total size of the space is 1 + 7 + 8 + 4 = 20, it is not a multiple of the number of section boundary of the structure (that is, the number of bytes occupied by the basic data type occupying the maximum space in the structure sizeof (double) = 8), so four bytes need to be filled, the value must be a multiple of sizeof (double) = 8.
Therefore, the total size of this structure is: sizeof (mystruc) is 1 + 7 + 8 + 4 + 4 = 24. Among them, 7 + 4 = 11 bytes are automatically filled by VC, and nothing makes sense.
Typedef struct {double I; int K [5]; char d [5]; char C;} date;
Struct data {short cat; Date cow; int dog ;};
Date Max;
Int A = sizeof (max); // 40
Int ndatasize = sizeof (data); // 56
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 satisfy the default alignment. Second, if n is less than the number of bytes occupied by the variable type, if the offset is a multiple of N, 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.
# Pragma pack (push) // save alignment status
# Pragma pack (4) // set to 4-byte alignment
Struct Test
{
Char M1;
Double M4;
Int m3;
};
# Pragma pack (POP) // restore alignment
The size of the above structure is 16. Next we will analyze its storage situation. First, we will allocate space for M1, and its offset is 0, which meets our own alignment (4-byte alignment ), m1 occupies 1 byte. Then we start to allocate space for M4. At this time, the offset is 1 and three bytes need to be supplemented. In this way, the offset must be a multiple of N = 4 (because sizeof (double) is greater than N ), m4 occupies 8 bytes. Then allocate space for M3. At this time, the offset is 12, which must be a multiple of 4. m3 occupies 4 bytes. At this time, space has been allocated for all member variables. A total of 16 bytes are allocated, which is a multiple of N. If you change # pragma pack (4) to # pragma pack (16), the size of the structure is 24.
2. sizeof usage Summary
In VC, sizeof has many usage and may cause some errors. Next, we will summarize the usage of sizeof based on the parameters following sizeof.
A. 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.
B. the parameter is an array or pointer. The following is an example.
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 a pointer.
// The size of a 32-bit system, of course, 4 bytes.
C. The parameter is a structure or class. The processing of sizeof applications in the class and structure is the same. However, there are two points to note: first, static members in the structure or class do not affect the size of the structure or class, because the storage location of static variables is irrelevant to the instance address of the structure or class.
Second, the structure without member variables or the size of the class is 1, because each instance of the structure or class must have a unique address in the memory.
The following is an example,
Class test {int A; static double c}; // sizeof (TEST) = 4.
Test * s; // sizeof (S) = 4, and S is a pointer.
Class test1 {}; // sizeof (test1) = 1;
D. The parameter is other. The following is an example.
Int func (char s [5]);
{
// Sizeof (s) will output 4 here, originally S is an array,
// However, because the system processes the parameter as a function as a pointer when it is passed, sizeof (s) actually calculates the pointer size.
Return 1;
}
Sizeof (func ("1234") = 4 // because the return type of func is int, it is equivalent
// Calculate sizeof (INT ).