Sizeof for C language keywords

Source: Internet
Author: User

The keyword sizeof in C language is an operator. The number of memory bytes of the returned object or type. The type is size_t (defined in <stddef. h>). There are two usage options:

  • Sizeof unary-expression
  • Sizeof (type-name)

When sizeof cannot be used:

  • An expression that has function type or an incomplete type
  • The parenthesized name of such a type
  • An expression that designates a bit-field member

If the operand type is VLA (variable length array), perform evaluate; otherwise, the evaluate is not required and the result is an integer constant.

Example 1 inter-process communication, such as storage allocator and I/O system

  * *dp = alloc( *dp);

Example 2 calculate the number of elements in the array

 array /  array[]

Example 3 VLA Size Calculation

 <stddef.h> b[n+];     b; = fsize3();    

 

Ansi c specifies that the bytes type is 1 byte. The returned results of other sizeof types are related to the compiler implementation. In a 32-bit system:

1. Basic Type

_ Bool 1

Char 1

Short 2

Int 4

Long 4

Long 8

Float 4

Double 8

Long double 12

_ Complex 16

Void 1

2. pointer

4

3. Enumeration

4

4. Array

Array length * array member size

5. struct

Byte ALIGN
Basic type member: SIZE

The offset of each member relative to the first address of the struct is an integer multiple of min (ALIGN, SIZE), and may be filled with Bytes between the members.
The total size of the struct is an integer multiple of min (ALIGN, SIZEmax), and may be filled with Bytes after the last member.

6. Join

The rule is similar to struct.

 

Test procedure:

 <stdio.h><complex.h> SIZEOF(x) printf("size of '%s' is %zd\n", #x, (x));  ); * (*)());  []);     SIZEOF( [ {});     SIZEOF( { { c;  s;});     SIZEOF( { i;  { i;  pack(push) pack(1) { i;  pack(pop) { d;  c;});  { c1:;  :;  c2:;});     SIZEOF( { i:;  c: { { i;  c;};  { { s1;  c;};  { c2;  { s1;  c1;};  c;  i;});      

 

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.