Use of the sizeof () function ------ [Badboy]

Source: Internet
Author: User
1. Use of sizeof: the sizeof operator provides the storage size of its operands in byte form. the sizeof operator cannot be used for function type, incomplete type or bit field. 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. 2. sizeof result: the result type of the sizeof operator is size_t.

1. Use of sizeof: the sizeof operator provides the storage size of its operands in bytes.

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.

2. Result of sizeof: the result type of the sizeof operator is size_t. in the header file, typedef is of the unsigned int type. This type ensures that it can accommodate the maximum object size.

The sizeof of int, unsigned int, short int, unsigned short, long int, unsigned long, float, double, and long double types are not specified in ansi c. The size depends on the implementation, generally, they may be 2, 2, 2, 4, 4, 4, 8, or 10.

When the operand is a pointer, sizeof depends on the compiler. 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.

When the operand has an array type, the result is the total number of bytes of the array, and the sizeof of the Union type operand is the maximum number of bytes of its members.

The sizeof of the structure type operand is the total number of bytes of this type of object, including any padding.

If the operand is an array parameter or a function-type parameter in the function, sizeof gives the pointer size.

Struct MyStruct

{

Char dda; // The offset is 0. the alignment is satisfied and the dda occupies 1 byte;

Double dda1; // the offset of the next available address is 1, not sizeof (double) = 8

// Multiple. you need to add 7 bytes to make the offset 8 (the alignment is satisfied ).

// Method). Therefore, VC automatically fills in 7 bytes, and dda1 is stored at an offset of 8

// Address, which occupies 8 bytes.

Int type; // the offset of the next available address is 16, which is twice that of sizeof (int) = 4.

// Number, which meets the alignment of int, so no VC auto-fill is required.

// Put the address at the offset of 16, which occupies 4 bytes.

}; // All member variables are allocated space. The total size of the space is 1 + 7 + 8 + 4 = 20, not a structure.

// Number of knots (that is, the number of bytes occupied by the largest space type in the structure sizeof

// (Double) = 8), so four bytes need to be filled to meet the structure size

// Sizeof (double) = multiple of 8

Therefore, the total size of this structure is: sizeof (MyStruc) is 1 + 7 + 8 + 4 + 4 = 24. Among them, 7 + 4 = 11 bytes are automatically filled by VC, without any meaningful content.

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.