Analyze Sizeof in C language

xiaoxiao2021-03-06  35

This article comes from: http: //www.china-pub.com Author: Chen Jian Yi (2001-08-11 13:05:00)

I. Concept of SIZEOF

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 a byte form.

The storage size of its operands. 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. 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 array type of unknown storage, unknown content or a combination, Void type, etc. If SIZEOF (MAX) is defined as int max (), sizeof (char_v) is defined if CHAR_V is defined as char char_v [max] and Max 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 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

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. (

Http://www.fanqiang.com) Enter [UNIX Forum]

supplement:

After defining your own structure in the program, the general compiler is installed 4 is the unit in units, so

The following situation:

Typedef struct a

{

Char a [3];

INT B;

}

The SIZEOF (A) operation result is 8, not 3 sizeof (int) = 4.

If you want to solve this problem, you can replenish alignment in your own definition, that is, change Char A [3] to CHAR [4], which is when the memory alignment is 4, SIZEOF (A) and a single element size and equalization.

Also added above

#program pack (push, 1)

The forced compiler is aligned according to the single byte, and can guarantee the SIZEOF results and individual elements and equalization.

转载请注明原文地址:https://www.9cbs.com/read-57980.html

New Post(0)