Skribent: Irrwahn Dato: Til: dng Emne: Re: [DNG] semantic of sizeof operator in C (was: simple-netaid from
scratch)
Hendrik Boom wrote on 12.06.19 14:40: > On Wed, Jun 12, 2019 at 01:47:42PM +0200, Irrwahn wrote:
>
>>
>> There is nothing wrong here. Gcc reports the size that is necessary to
>> store an object of type sesqui_int, including any padding that has been
>> applied, e.g. for alignment reasons. An array of n elements of that type
>> will in turn always be reported by sizeof as having *exactly* n times
>> that size, in bytes. Gcc is therefore in accordance with the language
>> definition.
>
> More precisely, sizeof(foo) is the spacing of consecutive elements of type foo.
>
> -- hendrik
Thank you Hendrik, that is indeed very aptly phrased!
Just for the sake of completeness, the actual language definition
takes the usual wordy but precise approach in Standardese:
ISO/IEC 9899:2011 | 6.5.3.4 The sizeof and _Alignof operators
| [...]
| 2 The sizeof operator yields the size (in bytes) of its operand,
| which may be an expression or the parenthesized name of a type.
| The size is determined from the type of the operand. The result
| is an integer. If the type of the operand is a variable length
| array type, the operand is evaluated; otherwise, the operand is
| not evaluated and the result is an integer constant.
| [...]
| 4 When sizeof is applied to an operand that has type char, unsigned
| char, or signed char, (or a qualified version thereof) the result
| is 1. When applied to an operand that has array type, the result is
| the total number of bytes in the array. 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.