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;});