6.5.3.4 The sizeof operator

Previous Table of Contents

1108 The sizeof operator shall not be applied to an expression that has function type or an incomplete type, to the parenthesized name of such a type, or to an expression that designates a bit-field member.

1109 The sizeof operator yields the size (in bytes) of its operand, which may be an expression or the parenthesized name of a type.

1110 The size is determined from the type of the operand.

1111 The result is an integer.

1112 If the type of the operand is a variable length array type, the operand is evaluated;

1113 otherwise, the operand is not evaluated and the result is an integer constant.

1114 When applied to an operand that has type char, unsigned char, or signed char, (or a qualified version thereof) the result is 1.

1115 When applied to an operand that has array type, the result is the total number of bytes in the array.85)

1116 When applied to an operand that has structure or union type, the result is the total number of bytes in such an object, including internal and trailing padding.

1117 The value of the result is implementation-defined, and its type (an unsigned integer type) is size_t, defined in <stddef.h> (and other headers).

1118 EXAMPLE 1 A principal use of the sizeof operator is in communication with routines such as storage allocators and I/O systems. A storage-allocation function might accept a size (in bytes) of an object to allocate and return a pointer to void. For example:


        extern void *alloc(size_t);
        double *dp = alloc(sizeof *dp);

The implementation of the alloc function should ensure that its return value is aligned suitably for conversion to a pointer to double.

1119 EXAMPLE 2 Another use of the sizeof operator is to compute the number of elements in an array:


        sizeof array / sizeof array[0]

1120 EXAMPLE 3 In this example, the size of a variable length array is computed and returned from a function:


        #include 
        
        size_t fsize3(int n)
        {
                char b[n+3];       // variable length array
                return sizeof b;   // execution time sizeof
        }
        
        int main()
        {
                size_t size;
                size = fsize3(10); // fsize3 returns 13
                return 0;
        }

1121 85) When applied to a parameter declared to have array or function type, the sizeof operator yields the size of the adjusted (pointer) type (see 6.9.1).

1122 Forward references: common definitions <stddef.h> (7.17), declarations (6.7), structure and union specifiers (6.7.2.1), type names (6.7.6), array declarators (6.7.5.2).

Next

Created at: 2005-06-29 02:18:59 The text from WG14/N1124 is copyright © ISO