Analyze Sizeof in C language
(Author: Fang Min, Wu Mingming, 2001 at 15:28 on January 8)
First, the concept of SizeOf is a single operator in the C language, such as other operators , - etc. It is not a function. The SIZEOF operator gives the storage size of its operand in byte. 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, the use of SIZEOF 1, used for data type sizeOf usage: sizeof (Type) data type must be hurt with parentheses. Such as SIZEOF (int). 2, used in variables SizeOf usage: sizeof (var_name) or sizeof var_name variable name can be horn without parentheses. Such as sizeof (var_name), sizeof var_name, etc. are all correct forms. Usage of brackets is more common, most programmers 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 unknown storage size, an unknown content of the content or a combination type, a Void type, and the like. 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, the result type of SIZEOF operator is SIZE_T, which in the header file TypeDef is 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, and the HUGE class pointer 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 put on the address of 4. 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 SIZEOF has a priority of Level 2, ratio /,% and other 3 operators. It can form an expression with other operators. Such as i * sizeof (int); where i is an int type variable. 5. SizeOf's main purpose 1, one of the SizeOf operators is to communicate with routines such as storage allocation and I / O systems.