Use of the sizeof () function —————— "Badboy"

Source: Internet
Author: User

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

The sizeof operator cannot be used for a function type, an incomplete type, or a bit field. An incomplete type is a data type with an unknown storage size, such as an array type of unknown storage size, structure or union type of unknown content, void type, and so on.

Result of 2.sizeof: The result type of the sizeof operator is size_t, which is a typedef of the unsigned int type in the header file. This type guarantees that the byte size of the largest object being built can be accommodated.

An int, a unsigned int, a short int, a unsigned short, a long int, a unsigned long, a float, a double, a long double type of sizeof are not specified in ANSI C, size dependent May be 2, 2, 2, 2, 4, 4, 4, 8, 10, respectively.

When the operand is a pointer, sizeof relies on the compiler. The near class pointer byte number is 2,FAR, and the huge class pointer byte number is 4. The average UNIX pointer byte count is 4.

When the operand has an array type, the result is the total number of bytes of the array, and sizeof, 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 of this type of object, including any padding.

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.

struct MYSTRUCT

{

The Char dda;//offset is 0, which satisfies the alignment and the DDA occupies 1 bytes;

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

Multiples of 7 bytes to make the offset to 8 (to satisfy the alignment

), so VC automatically fills 7 bytes, dda1 is stored at an offset of 8

Address, it occupies 8 bytes.

int type;//The offset of the next available address is 16, which is the number of sizeof (int) =4

number, which satisfies the alignment of int, so no VC auto-fill is required, type is stored

Placed at an offset of 16, it occupies 4 bytes.

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

The number of section boundaries (that is, the number of bytes in the structure that occupy the largest space in the struct. sizeof

(double) =8), so a multiple of 4 bytes is required to satisfy the size of the structure to

Multiple of sizeof (double) =8

So the total size of the structure is: sizeof (MYSTRUC) is 1+7+8+4+4=24. One of the total 7+4=11 bytes is the VC automatically filled, not put anything meaningful.

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.