Parse sizeof in C language-general Linux technology-Linux programming and kernel information. The following is a detailed description.
I. sizeof Concept
Sizeof is a type of single object operator in C language, such as other operators ++ and -- in C language. It is not a function. The sizeof operator provides the storage size of its operands in bytes. The operand can be an expression or a type name enclosed in parentheses. The storage size of the operands is determined by the type of the operands.
Ii. Usage of sizeof
1. Used for Data Types
Sizeof usage: sizeof (type)
The data type must be enclosed in parentheses. For example, sizeof (int ).
2. Used for variables
Sizeof format: sizeof (var_name) or sizeof var_name
Variable names can be enclosed in parentheses. For example, sizeof (var_name) and sizeof var_name are all in the correct format. Brackets are more commonly used. Most programmers use this form.
Note: the sizeof operator cannot be used for function types, which are incomplete types or bit fields. An incomplete data type refers to a data type with an unknown storage size, such as an array type with an unknown storage size, an unknown content structure, a union type, and a void type.
For example, if sizeof (max) is defined as int max () and sizeof (char_v), if char_v is defined as char char_v [max] and MAX is unknown, sizeof (void) are not in the correct format.
Iii. sizeof results
The result type of the sizeof operator is size_t, which is in the header file Typedef is of the unsigned int type. This type ensures that it can accommodate the maximum object size.
1. If the operand has a char, unsigned char, or signed char type, the result is equal to 1.
Ansi c officially specifies that the character type is 1 byte.
2. the sizeof types of int, unsigned int, short int, unsigned short, long int, unsigned long, float, double, and long double are not specified in ansi c, the size depends on the implementation, which may be 2, 2, 2, 4, 4, 4, 8, or 10.
3. When the operand is a pointer, sizeof depends on the compiler. For example, in Microsoft C/C ++ 7.0, the number of near class pointers is 2, and the number of far and huge class pointers is 4. Generally, the number of Unix pointer bytes is 4.
4. When the operand has an array type, the result is the total number of bytes of the array.
5. the sizeof of the Union type operand is the maximum number of bytes of its member. The sizeof of the Structure Type operand is the total number of bytes of this type of object, including any padding.
Let's look at the following structure:
Struct {char B; double x;};
On some machines, sizeof (a) = 12, while generally sizeof (char) + sizeof (double) = 9.
This is because when the compiler considers alignment, it inserts vacancies in the structure to control the address alignment of each member object. For example, the double-type structure member x should be placed in the address divisible by 4.
6. If the operand is an array parameter in the function or a function-type parameter, sizeof gives the pointer size.
Iv. Relationship between sizeof and other operators
The priority of sizeof is Level 2, which is higher than Level 3 operators such as/and %. It can be used together with other operators to form an expression. For example, I * sizeof (int); where I is an int type variable.
V. main use of sizeof
1. One major purpose of the sizeof operator is to communicate with routines such as storage allocation and I/O systems. For example:
2. Another major purpose of sizeof is to calculate the number of elements in the array. For example:
Void * memset (void * s, int c, sizeof (s )).
Vi. Suggestions
Since the number of bytes of the operand may change during implementation, we recommend that you use ziseof to replace constant calculation when it comes to the size of the operand bytes.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.