Analyze the SizeOf in the C language from: http://www.china-pub.com Jian Yi 2002 January 08, 14:02, Sizeof concept
SIZEOF is a single operator in C language, such as other operators of C language , - etc. It is not a function. The SIZEOF operator gives the storage size of its operands in bytes. The number of operands can be an expression or a type name enclosed in parentheses. The storage size of the operand is determined by the type of operand.
Second, SIZEOF how to use
1, used for data type
SIZEOF use form: sizeof (Type)
The data type must be enclosed in parentheses. Such as SIZEOF (int).
2, used for variables
SizeOf use form: sizeof (var_name) or sizeof var_name
The variable name can be horn without parentheses. Such as sizeof (var_name), sizeof var_name, etc. are all correct forms. The usage of brackets is more common, and most numbers use this form.
Note: The SIZEOF operator cannot be used for function types, incomplete types or bit fields. Incomplete type refers to a data type with unknown storage size, such as an array type of unknown storage, unknown content or a combination, Void type, etc.
If SIZEOF (MAX), if the variable max is defined as int max (), sizeof (char_v) If CHAR_V is defined as char char_v [max] and Max is unknown, SizeOf (Void) is not correct.
Third, SIZEOF results
The result type of the SIZEOF operator is Size_T, which in the header file is typedef for the unsigned int type. This type ensures that the byte size of the maximum object established can be accommodated.
1. If the operand has a type char, unsigned char or signed char, the result is equal to 1.
ANSI C officially specifies that the character type is 1 byte.
2, int, unsigned int, unsigned long, flos 2, 4, 4, 4, 8, 10.
3. When the operand is a pointer, SIZEOF depends on the compiler. For example, in Microsoft C / C 7.0, the NEAR class norm is 2, FAR, the HUGE pointer byte is 4. General Unix's pointer one is 4.
4. When the operand has an array type, the result is the total number of bytes of arrays.
5. SIZEOF of the joint type operand is the number of bytes of its maximum byte member. SIZEOF of the structure type operand is the total number of bytes of this type of object, including any pad.
Let us see the following structure:
Struct {char b; double x;} a;
SIZEOF (A) = 12 on some machines, and general SizeOf (char) sizeof (double) = 9.
This is because the compiler is inserted into the space in the structure in the structure to control the address alignment of each member object when considering alignment problems. If the Double type of structural member X is to be placed in 4 complete addresses.
6. If the operand is the shape parameter or function type in the function, SIZEOF gives the size of its pointer.
Fourth, the relationship between SIZEOF and other operators
The priority of SizeOf is 2, the ratio /,% 3 operators are high. It can form an expression with other operators. Such as i * sizeof (int); 中 I is an Int type variable.
5. Main uses of SizeOf
1. A main purpose of the SIZEOF operator is to communicate with the routine like the storage assignment and the I / O system. For example: void * malloc (size_t size),
SIZE_T FREAD (Void * PTR, SIZE_T SIZE, SIZE_T NMEMB, FILE * stream).
2, another main purpose of SizeOf is to calculate the number of elements in arrays. E.g:
Void * MEMSET (Void * S, INT C, SIZEOF (S)).
Six, suggestions
Since the number of bytes of operands may change when implementation, it is recommended to replace constant calculations in ZISEOF when it involves an operational digital size.