To] parse sizeof in the C language

Source: Internet
Author: User
Tags microsoft c

Analysis of sizeof in C language

First, the concept of sizeof

sizeof is a single-eye operator of C, such as the other C language operators + + 、--. It is not a function. The sizeof operator is given in bytes
The storage size of its operands. The operand can be an expression or a type name enclosed in parentheses. The storage size of the operand is determined by the type of the operand.


Second, the use of sizeof method

1. For data type

sizeof use form: sizeof (type)

The data type must be enclosed in parentheses. such as sizeof (int).

2. For variables

sizeof use form: sizeof (var_name) or sizeof Var_name

Variable names can be enclosed without parentheses. such as sizeof (Var_name), sizeof var_name, etc. are the correct form. The use of parentheses is more common, and most
A number of programmers take this form.

Note: The sizeof operator cannot be used for function types, incomplete types, or bit fields. An incomplete type refers to a data type with an unknown storage size, such as an unknown
The array type that stores the size, the structure or union type of the unknown content, the void type, and so on.

such as 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 the correct form.


Iii. Results of sizeof

The result type of the sizeof operator is size_t, which is a typedef of type unsigned int in the header file. This type guarantees the best possible
The byte size of the large object.

1. If the operand has a type char, unsigned char, or signed char, the result is equal to 1.

ANSI C formally stipulates that the character type is 1 bytes.

2, int, unsigned int, short int, unsigned short, long int, unsigned long, float, double, long double
The type of sizeof is not specified in ANSI C, and the size depends on the implementation, which may be 2, 2, 2, 2, 4, 4, 4, 8, 10, respectively.

3. sizeof relies on the compiler when the operand is a pointer. For example, in Microsoft c/c++7.0, the near class pointer byte count is 2,FAR, huge class pointer
The number of bytes is 4. The average UNIX pointer byte count is 4.

4, when the operand has an array type, the result is the total number of bytes in the array.

5. SizeOf of the Union type operand is the number of bytes of its largest byte member. The sizeof of the struct type operand is the total number of bytes for this type of object, including
Any cushion to fill in.

Let's look at the following structure:

struct {char b; double X;} A;

On some machines sizeof (a) = 12, while the general sizeof (char) + sizeof (double) = 9.

This is because the compiler, when considering alignment issues, inserts a vacancy in the structure to control the address alignment of each member object. A struct member such as a double type x to put
The address that is divisible by 4.

6, if the operand is an array parameter in a function or a formal parameter of a function type, sizeof gives the size of its pointer.


Iv. the relationship between sizeof and other operators

sizeof has a priority level of 2, and a 3-level operator with a higher precedence than/,%, and so on. It can form an expression together with other operators. such as i*sizeof (int);
Medium I is an int type variable.


V. Main uses of sizeof

1. One of the main uses of the sizeof operator is to communicate with routines such as storage allocation and I/O systems. For example:

void *malloc (size_t size),

size_t fread (void * ptr,size_t size,size_t nmemb,file * stream).

2. The other main use of sizeof is to calculate the number of elements in the array. For example:

void * memset (void * S,int c,sizeof (s)).


VI. recommendations

Because the number of bytes of the operand may change when implemented, it is recommended to use ziseof instead of constant calculation when it comes to the size of the operand byte.

To] parse sizeof in the C language

Related Article

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.