C language detailed sizeof

Source: Internet
Author: User
Tags microsoft c

First, the concept of sizeof

sizeof is a single-eye operator of C, such as the other C language operators + + 、--.

It is not a function. The sizeof operator gives the storage size of its operands in bytes. The operand can be an expression or a type name enclosed in parentheses. the storage size of the operand is determined by the type of the operand.

Second, the use of sizeof method

1. For data type

sizeof use form: sizeof (type)
Data types must be enclosed in parentheses: 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, etc. are all in the right formthe use of parentheses is more common, and most programmers take this form.

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 unknown storage size array type, unknown content structure or union type, void type, and so on.
For example: sizeof (max)- - when the variable max is defined as int max (); sizeof (CHAR_V) --when CHAR_V is defined as Char Char_v[max] and MAX is unknown, sizeof (void)These are not the correct forms.

Third, the results of sizeof (the following results are obtained in the Linux v2.6 gcc v4)

The result type of the sizeof operator is size_tIt is defined in the header file as: typedef unsigned int size_t;This type guarantees that the byte size of the largest object being built can be accommodated.

1, ANSI C formally stipulates that the character type is 1 bytes.
sizeof (char) = 1; sizeof (unsigned char) = 1; sizeof (signed char) = 1;
  
2. Other types are not specified in ANSI C, and size depends on implementation.
sizeof (int) = 4;sizeof (unsigned int) = 4;sizeof (short int) = 2;sizeof (unsigned short) = 2;sizeof (long int) = 4;sizeof (unsigned long) = 4;sizeof (float) = 4;sizeof (double) = 8;sizeof (long double) = n;
3. SizeOf relies on the compiler when the operand is a pointer.    in Microsoft c/c++7.0, the near class pointer byte count is 2,FAR, huge class pointer byte number is 4. the number of pointer bytes for general Unix/linux is 4. For example: Char *p; In Linuxsizeof (p) = 4;

4, when the operand has an array type, the result is the total number of bytes in the array.  Example: Char a[5];int b[5];sizeof (a) = 5;sizeof (b) =;   5, when the operand is a specific string or value, the corresponding conversion according to the specific type.  For example: sizeof (8) = 4; automatic conversion to int typesizeof (8.8) = 8; Automatic conversion to double type, note, not float typesizeof ("ab") = 3//auto-Convert to array type,//length is 4, not 3, because the last ' \ n ' symbol is added//There is information that will automatically be converted to pointer type (Linux is 4)//may have a relationship with the operating system and the compiler
6. When the operand is a union type, sizeof is the number of bytes of its largest byte member. When the operand is a struct type, sizeof is the total number of bytes of its member type, including the supplemental bytes.     Let's take an example to say:Union u{//For Unionchar c;double D;}u;sizeof (U) = max (sizeof (c), sizeof (d)) = sizeof (1,8) = 8;
struct a{//for structsChar b;double x;}a;
On Linux: sizeof (a) = N;and generally sizeof (char) + sizeof (double) = 9;
This is because the compiler, when considering alignment issues, inserts a vacancy in the structure to control the address alignment of each member object. But if fully aligned, sizeof (a) = 16, because B is placed at an offset of 0, accounting for 1 bytes;when storing x, the double type has a length of 8 and needs to be placed on an offset that is divisible by 8, which requires 7 empty bytes .reaches 8, when the offset is 8, and the length after X is 16. in this example, all struct members are placed in an address that is divisible by 4 (the way Linux is stored), where it is 3 bytes, so it is 12.

7, when the operand is an array parameter in a function or a formal parameter of a function type:   sizeof gives the size of its pointer, with a value of 4 in Linux.

Iv. the relationship between sizeof and other operators

sizeof has a priority level of 2, and a 3-level operator with a higher precedence than/,%, and so on. It can form an expression together with other operators:For example: int i = ten;i * sizeof (int);
v. Main uses of sizeof

1. The main purpose is to communicate with routines such as storage allocation and I/O systems.  For example: void *malloc (size_t size);
size_t fread (void *ptr, size_t size, size_t nmemb, FILE * stream);

2, the other main purpose is to calculate the number of elements in the array.  For example: void *memset (void *s, int c, sizeof (s));

Finish

C language detailed sizeof

Related Article

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.