:: Re: [DNG] semantic of sizeof operat…
Top Pagina
Delete this message
Reply to this message
Auteur: Didier Kryn
Datum:  
Aan: dng
Onderwerp: Re: [DNG] semantic of sizeof operator in C
Le 12/06/2019 à 16:29, Irrwahn a écrit :
>> 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.
>

    Thanks guys for the neat clarification, in particular that the
stucts and unions shall actualy comprise trailing padding bytes when
required by alignment. BTW I didn't know of the _Alignof operator. And,
sure, for VLAs, the size it evaluated at run time. I'm a big fan of VLAs
for dynamic allocation in the stack.

        Didier