Re: sizeof(type)

Roland Illig <[email protected]>
Newsgroups gmane.os.netbsd.devel.userlevel
Message-ID <[email protected]>
Am 25.11.2024 um 17:26 schrieb Edgar Fuß:
> Why does C's sizeof operator need parentheses when applied to a type?
>
> I suspect some ambiguity without the parentheses, but I fail do make up one.

The expression 'sizeof int * + 1' has two different interpretations:

sizeof(int) * (+1)
sizeof(int *) + 1

I found this out by running:

bison -o /dev/null -v --report-file=before usr.bin/xlint/lint1/cgram.y

Then I removed the parentheses from the rule 'T_SIZEOF T_LPAREN
type_name T_RPAREN' and ran Bison again.

There were 23 new conflicts. 21 of these were due to the keywords
'_Alignas', '__packed' and '__attribute__', and these are ambiguous in
many other places of the lint grammar, so I treated them as unsuspicious.

Two of the newly added conflicts were about the T_ASTERISK '*' token.
These gave me enough of a hint to come up with the above expression.

Roland
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.