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, not a full type, or a bit field. Not all types refer to data types with unknown storage size, such as array types with 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 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 To achieve. The general 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 of the union type operand is the number of bytes of its largest byte member.

The struct type operand of sizeof is the total number of bytes of such a type object, including whatever padding is included.

The operand is assumed to be an array of parameters in a function or a function type, and 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

A multiple of 7 bytes is required to make the offset to 8 (satisfies the alignment

), so the VC itself is actively populating 7 bytes. DDA1 stored at an offset of 8

's 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, to meet the alignment of int, so do not need to actively fill the VC, type storage

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

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

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

A multiple of (double) =8). So you need to populate 4 bytes. To meet the size of the structure

Multiple of sizeof (double) =8

So the total size of the structure is: sizeof (MYSTRUC) is 1+7+8+4+4=24. The total of 7+4=11 bytes is VC self-filled, no matter what meaningful things.

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

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.