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, please see the following structure: struct mystruct {double dda1; char dda; int type}; what results in using SIZEOF in the structure ?truct? How much is SIZEOF (MySTRUCT)? Maybe you will seek this: sizeof (mYStruct) = sizeof (Double) sizeof (char) sizeof (int) = 13 But you will find sizeof (mystruct) to 16 when testing the size of the above structure in the VC. 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. Type alignment (variable storage start address of the start address of the structure) CHAR offset must be a multiple of SizeOf (CHAR) 1 multiplier INT offset must be SIZEOF (int) 4 multiples The Float offset must be SizeOf (Float), the double double Double offset must be a multiple of the SizeOf (Double). The order in which it appears in the structure sequentially applies space, while adjusting the position according to the above alignment, the empty byte VC 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 the space is allocated to 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 and structure The start address is the same (just the unity of the offset 0 is just a multiple of sizeof (double), the member variable takes up SIZEOF (Double) = 8 bytes; next for the second member DDA allocation space, then the next one can be assigned The address of the start address of the structure is 8, which is a multiple of sizeof (char), so that the DDA is stored in a place to be 8, which occupies SizeOf (char) = 1 Bytes; next to the third member TYPE allocation space, then the next one can be assigned to the offset of the structure of the structure is 9, not the multiple of sizeof (int) = 4, in order to meet alignment The constraint problem of the transmodation, the VC automatically fills 3 bytes (these three bytes do not make anything), then the next one can allocate the offset of the start address of the structure 12, just sizeof (int ) = 4 times, so the TYPE is stored in the place of the offset 12, the member variable takes up SIZEOF (int) = 4 bytes; at this time, the member variables of the entire structure have allocated space, total occupied The space size is: 8 1 3 4 = 16, just the number of byte boundaries of the structure (ie, the number of bytes occupying the maximum space occupied by the structure, there is no vacancy The byte needs 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 variable of the above MyStruct, so that it becomes the following: Struct MyStruct {char DDA; Double DDA1; INT TYPE}; how much 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 // multiple, need to make up 7 bytes to make the offset amount be 8 (satisfied alignment // mode), so the VC automatically fills 7 bytes, and the DDA1 stores the address of 8 // On, it occupies 8 bytes. INT TYPE; / / The offset of the next available address is 16, which is the multiplier of sizeof (int) = 4, satisfying the INT alignment, so no VC automatic fill, Type save // put it in part On the address of 16, it occupies 4 bytes.
}; // All member variables allocate space, the total size of 1 7 8 4 = 20, not the number of bounds of the structure //, that is, the number of bytes occupied by the maximum space in the structure The multiple of sizeof // (Double) = 8), so you need to fill 4 bytes to meet the size of the structure to // sizeOf (double) = 8 multiple. 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, then the total size of the structure must be a multiple of the number of space occupied by the maximum space; otherwise you must The multiple of N. The following is an example of its usage. #pragma pack (push) // Save Align Status #pragma Pack (4) // Set to 4 bytes Align Struct Test {Char M1; Double M4; Int M3;}; #pragma Pack (POP) // Restore Alignment The size of the above structure is 16, and the storage case is analyzed below. First, the m1 allocation space is assigned, the offset is 0, satisfying our own alignment (4 bytes alignment), and M1 take up 1 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 yourself by readers) 2, SizeOf usage summarizes in VC, SizeOf has many 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 group accounts for int * a = new int [50]; // sizeof (a) = 4; a For a pointer, SIZEOF (a) is the size of the pointer //, 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.