This paper mainly includes two parts, the first partial focus is introduced in the VC, how to use SizeOf to see the size of the structure, and the problem that is easy to appear, and give a solution method, the second part summarizes the main usage of SizeOf in the VC. .
1, SIZEOF application in structure
Please see the following structure:
Struct MyStruct
{
Double DDA1;
Char DDA;
Int Type
}
What results do you have to use SIZEOF to the structure ?truct? How much is SIZEOF (MySTRUCT)? Maybe you will seek this:
Sizeof (mYStruct) = sizeof (double) sizeof (char) sizeof (int) = 13
But when you test the size of the above structure in the VC, you will find SizeOf (MyStruct) to 16. Do you know why you will find such a result in VC?
In fact, this is a special process of VC on variable storage. In order to improve the storage speed of the CPU, VC is "aligned" processing on the starting address of some variables. By default, the VC specifies the starting address of each member variable to store the start address of the structure must be a multiple of the number of bytes occupied by the type of variable. The alignment (VC6.0, 32-bit system) of the common type is listed below.
Types of
Alignment (the offset of the start address of the variable is relative to the starting address of the structure)
Charr
The offset must be a multiple of SizeOf (char) 1
int
The offset must be a multiple of SizeOf (int) 4
Float
The offset must be a multiple of SizeOf (Float)
Double
The offset must be a multiple of Sizeof (double)
Short
The offset must be a multiple of Sizeof (Short)
Each member variable is sequentially applied in sequence according to the order in the structure, while adjusting the position according to the above alignment, and the byte VC of the vacancy will automatically populate. At the same time, VC is multiple of the number of byte boundary of the structure of the structure (ie, the number of bytes occupying the maximum space occupied by the maximum space), it will automatically be automatically applied to the last member variable. Fill the byte of the vacant.
The following examples use the previous example to explain how the VC is stored.
Struct MyStruct
{
Double DDA1;
Char DDA;
Int Type
}
When allocating space for the above structure, the VC is allocated to the first member DDA1 allocated space according to the order and alignment of the member variable, and the start address of the start address is the same (just the offset 0 is just Sizeof). (double), the member variable takes up SIZEOF (Double) = 8 bytes; next for the second member DDA allocation space, then the next one can allocate the offset of the start address of the structure 8. It is the multiple of sizeof (char), so the DDA is stored in the place where the offset is 8 satisfies the alignment. The member variable takes up SIZEOF (CHAR) = 1 byte; next to the third member TYPE allocation space At this time, the next one can allocate the offset of the start address of the structure is 9, not the multiple of sizeof (int) = 4, in order to satisfy the alignment to the offset constraint, VC automatically fills 3 bytes (These three bytes do not have anything), then the next device can be allocated to 12 of the offset of 12, just a multiple of sizeof (int) = 4, so putting Type in offset Where is 12, the member variable takes up SIZEOF (int) = 4 bytes; at this time, the members variables of the entire structure have allocated space, the total occupied space size is: 8 1 3 4 = 16 , Just a multiplication of the number of bytes boundaries (ie, the number of bytes occupying the maximum space occupying the maximum space), so no vacant bytes need to be filled. So the size of the entire structure is: sizeof (mYStruct) = 8 1 3 4 = 16, where there are 3 bytes of VC automatically filled, no meaningful things. Let's take an example below to exchange the position of the member variables of the above MyStruct, so that it becomes the following:
Struct MyStruct
{
Char DDA;
Double DDA1;
Int Type
}
What is the space occupied by this structure? In a VC6.0 environment, SIZEOF (MyStruc) can be obtained. In conjunction with some of the principles of the allocation space mentioned above, how how the VC is allocated to the above structure. (Brief description)
Struct MyStruct
{
Char DDA; // Offset is 0, satisfying alignment, DDA occupies 1 byte;
Double DDA1; / / The offset of the next available address is 1, not sizeof (double) = 8
// The multiple, it is necessary to make up 7 bytes to make the offset be 8 (satisfied alignment)
// mode), so the VC automatically fills 7 bytes, and the DDA1 is stored in the offset of 8.
// The address is on, it takes 8 bytes.
INT TYPE; / / The offset of the next available address is 16, which is the multiplier of sizeof (int) = 4
// Number, meet int gaze, so you don't need VC automatic fill, Type
// On the address of the offset 16, it occupies 4 bytes.
}; // All member variables are allocated, the total size is 1 7 8 4 = 20, not structure
// The number of bounds (ie, the number of bytes occupying the maximum space in the structure) SIZEOF
/ / (double) = 8) The multiple of the number, so 4 bytes need to meet the size of the structure.
// SizeOf (double) = 8 multiples. So the total size of this structure is: sizeof (mYStruc) is 1 7 8 4 4 = 24. The total 7 4 = 11 bytes are VC automatic filled, and there is no meaningful thing.
The special processing of the VC to the structure does increase the speed of the CPU storage variable, but sometimes it brings some trouble, we also mask the default alignment of the variable, you can set the alignment of the variable.
#Pragma pack (n) is provided in the VC to set the variable to align in the n-byte alignment. N-byte alignment is that there are two cases of the offset of the start addresses of the variable storage: First, if n is greater than or equal to the number of bytes occupied by this variable, the offset must meet the default alignment, second If N is less than the number of bytes occupied by the type of the variable, then the offset is a multiple of N, which does not need to satisfy the default alignment. The total size of the structure also has a constraint condition, divided into 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 number of space occupied by the maximum space of space;
Otherwise, the multiple must be n. The following is an example of its usage.
#pragma pack (push) // Save the alignment status
#pragma pack (4) // Set to 4 bytes alignment
Struct Test
{
Char m1;
Double M4;
INT M3;
}
#pragma pack (pop) // Restore alignment status
The size of the above structure is 16, and the storage case is analyzed below. First, the m1 allocation space, the offset is 0, satisfying the alignment of our own settings (4 bytes alignment), and M1 take up one byte. Then start to allocate space for the M4. At this time, its offset is 1, which requires complement of 3 bytes, which makes the offset multiple multiple of N = 4 (because sizeof (double is greater than n), M4 occupies 8 words Section. Then, the m3 allocation space, then its offset is 12, the number of four is satisfied, and the m3 occupies 4 bytes. At this time, it has been assigned a space for all member variables, allocated 16 bytes, satisfying a multiple of n. If we change the top #pragma pack (4) to #pragma Pack (16), then we can get the size of 24. (Please analyze your own yourself)
2, SIZEOF usage summary
In VC, SizeOf has a lot of usage and it is easy to cause some errors. The following is summarized according to the parameters behind SizeOf.
A. The parameter is a data type or is a general variable. For example, SizeOf (int), SizeOf (long), etc. This situation should be noted that the results obtained by different system systems or different compilers may be different. For example, the int type accounts for 2 bytes in the 16-bit system, and 4 bytes in the 32-bit system.
B. The parameter is an array or a pointer. The following is an example.
INT A [50]; // sizeof (a) = 4 * 50 = 200; the space size of the number of groups
INT * a = new int [50]; // sizeof (a) = 4; A is a pointer, SIZEOF (a) is a pointer
// Size, in the 32-bit system, of course, 4 bytes.
C. The parameter is a structure or class. The SIZEOF application is the same in the processing of classes and structures. However, there are two points to note that static members in the first, structural or class do not affect the size of the structure or class, as the storage location of the static variable is independent of the instance address of the structure or class.
Second, there is no size of the structure or class of a member variable is 1, because each of the structures or classes must be guaranteed
An instance has a unique address in memory.
Below will now be described,
Class test {Int a; static double c}; // sizeof (test) = 4.Test * S; // sizeof (s) = 4, s is a pointer.
Class test1 {}; // sizeof (test1) = 1;
D. The parameter is other. The following is an example.
Int Func (Char S [5]);
{
Cout << SizeOf (s); // This will output 4, the original S is an array, but because of the letter
// The number of parameters is transmitted as a pointer when transmitting.
// In actually the size of the pointer is actually in sizeof (s).
Return 1;
}
SIZEOF (Func ("
1234 "
)) = 4 // Because the return type of FUNC is int, it is equivalent to
// Ask SIZEOF (int).
The above is the basic usage of SIZEOF, pay attention to the assignment strategy of the allocation variable of VC in practice, so that some errors can be avoided.