"sizeof"

Source: Internet
Author: User

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

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 and so on are all the right forms. The use of parentheses is more common, and most processes
This form is used by the order clerk.

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,
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 at this time Char_v
[Max] and Max unknown, sizeof (void) is not the correct form.

Iii. Results of sizeof
The result type of the sizeof operator is size_t, which in the header file is a typedef of
unsigned int type. This type guarantees the ability to accommodate the largest object that is built
The size of the byte.

1. If the operand has a type char, unsigned char, or signed Char, its
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
sizeof of type Double is not specified in ANSI C, and size depends on
implementations, 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. such as Microsoft
c/c++7.0, the number of pointer bytes in the near class is 2,far, huge class pointer bytes
to 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. Structure class
Operands of type operand are 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;} 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 slots in the structure to control
The address of each member object is aligned.
A struct member x, such as a double type, is placed at an 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.
int func (char s[5]);
{
Cout<<sizeof (s);//This will output 4, originally s as an array, but
As the parameters of the function are passed
Processing is a pointer, so sizeof (s)
To find the size of the pointer.
return 1;
}

sizeof (func ("1234")) =4//because the return type of func is int, so the phase
When asked for sizeof (int).


7. Parameters are structs or classes. sizeof applications are the same in class and struct processing.
But there are two points to note,
A static member of a struct or class does not produce the size of a struct or class
Because static variables are stored in the same location as the structure or the instance address of the class
Independent.
Second, the size of the struct or class with no member variable is 1, because the knot must be guaranteed
Each instance of a construct or class has a unique address in memory.
The following example shows that
Class test{int a;static Double c};//sizeof (Test) =4.
Test *s;//sizeof (s) =4,s is a pointer.
Class test1{};//sizeof (test1) = 1;

"sizeof"

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.