Sizeof is surprisingly difficult to parse in C

Posted by jandeboevrie 1 day ago

Counter31Comment13OpenOriginal

Comments

Comment by wren6991 1 day ago

> (everything here also applies to c2y's newly introduced _Countof)

Stop the press, they're adding what? So I don't have to keep adding #define count_of(arr) (sizeof(arr) / sizeof(*arr)) to every codebase and living with its failure modes?

I'm really loving C's character arc at the moment, where they keep going back to fix old problems instead of piling everything and the kitchen sink into the language.

Comment by zbentley 1 day ago

> I'm really loving C's character arc at the moment, where they keep going back to fix old problems

Valid, but it does seem a bit Stockholm Syndrome to have this much rejoicing that your language added a builtin for getting the length of an array in 2026.

Comment by pjmlp 11 hours ago

If only they actually fixed arrays and strings properly.

C23 and C2y have plenty of kitchen sink features.

Comment by HexDecOctBin 1 day ago

Yeah, despite puritanical hand-wringing over C23, I do like both the discipline and the selectivity with WG14 has shows (and I say this as a card-carrying C++ hater).

I do wish they will start looking into proper arrays since there is already a syntax for this can can be augmented semantically:

    int func (int arg[static 5])

Comment by uecker 23 hours ago

Can you explain what you mean exactly regarding this augmenting this syntax?

Comment by wren6991 22 hours ago

I guess they are saying sizeof(...) should return the annotated static size rather than sizeof(int*) in this case

Comment by glouwbug 1 day ago

It's a unary operator, kind of like ~ or !, which can apply to parenthesis (), until it meets a type, then the parenthesis are mandatory:

    int x;
    sizeof x;     // okay
    sizeof (x);   // okay
    sizeof int;   // not okay
    sizeof (int); // okay
It's a weird double rule to parse, but even worse, the strongest binding precedence typically doesn't like an alpha identifier at that point in the recursive descent, so you get weird rules like:

    read_prefix():
        is peak() '!': ...
        is peak() '~": ...
        is alpha(peak())
            maybe alpha is sizeof?
                wait, it wasn't sizeof? it was an identifier? long rewind
        is peak() '+': ...
Something like @ would've been a good pick for sizeof.

\ramble on

(actually, better yet, it should've been used instead of & for taking the address). Imagine:

    int x;
    int* p = @x;
\ramble off

Comment by atiedebee 11 hours ago

Ive actually toyed with the idea of having @ as a pointer dereference. It seems to make sense: you want to know what is at the address stored in p.

Comment by 1 day ago

Comment by high_na_euv 1 day ago

I feel like what you struggle with is expression parsing, not sizeof itself?

Comment by inigyou 1 day ago

Wait, they added a new operator and didn't fix that parsing stupidity by always requiring parentheses?

Comment by adrian_b 1 day ago

I assume that there would be a zillion of legacy C source files that would be broken by such a change.

Comment by inigyou 1 day ago

There are no legacy C source files using _Countof.

Comment by uecker 1 day ago

That was discussed, but consistency with sizeof was considered more important. And if you figured out how to parse sizeof, parsing countof is not an additional burden.