News for hackers. Stuff that matters.
Re:My personal C coding style as of late 2023 by thetic on Sunday October 08, 2023 @09:51PM (#37816126)
> #define sizeof(x) (size)sizeof(x)

Undefined behavior[1]

> #define assert(c) while (!(c)) __builtin_unreachable()

Undefined behavior[1]

> I’ll cast away the const if needed.

Undefined behavior[2]

> The assignments are separated by sequence points, giving them an explicit order.

I don't believe assignments are sequence points and only the function call is.

[1] https://en.cppreference.com/w/c/language/identifier#Reserved...

[2] https://en.cppreference.com/w/c/language/const

Re:My personal C coding style as of late 2023 by LegionMammal978 on Sunday October 08, 2023 @10:32PM (#37816362)
>> I’ll cast away the const if needed.

> Undefined behavior[2]

How so? As the page you linked mentions, simply casting 'const T *' to regular 'T *' is well-defined; it's only modifying a const object through the pointer that's UB (C17 6.7.3/7).

> I don't believe assignments are sequence points and only the function call is.

Assigments within expressions don't create sequence points. However, the expression of an expression statement is a full expression (i.e., not a subexpression of another expression), and there is a sequence point between each pair of full expressions (C17 6.8/4). In other words, the semicolons create sequence points.

Re:My personal C coding style as of late 2023 by WalterBright on Sunday October 08, 2023 @09:58PM (#37816167)
> #define assert(c) while (!(c)) __builtin_unreachable()

And people keep telling me that nobody uses the C preprocessor to define their own syntax any more!

Re:My personal C coding style as of late 2023 by dundarious on Sunday October 08, 2023 @10:16PM (#37816276)
> > I’ll cast away the const if needed.

> Undefined behavior[2]

To be clear, it's only UB if the object was defined const, which is the case given he wrote:

> One small exception: I still like it as a hint to place static tables in read-only memory closer to the code. I’ll cast away the const if needed.

So you are correct on this point. Funnily enough, such objects are relatively rare IME, so I had to double-check to see that he was advocating it in the rare case where it must not be applied.

Re:My personal C coding style as of late 2023 by comex on Sunday October 08, 2023 @10:16PM (#37816277)
> Undefined behavior[2]

Given that this particular undefined behavior usually causes crashes in practice, I expect the author is talking about casting away the const but not actually writing to the pointer. Which is legal.

Re:My personal C coding style as of late 2023 by paulddraper on Sunday October 08, 2023 @10:35PM (#37816372)
Or a common situation is mutable -> const -> mutable.

And that is legal.

Re:My personal C coding style as of late 2023 by thetic on Sunday October 08, 2023 @10:25PM (#37816325)
The legal cases in which he needs to cast away const could be avoided if the arguments to called functions appropriately qualified.
Re:My personal C coding style as of late 2023 by LegionMammal978 on Sunday October 08, 2023 @10:35PM (#37816377)
He never said he needs to cast away const to do what he is attempting to do, he just said that he wants to cast away const to reduce clutter, even though the program would have the same semantics as if he kept the const.
Re:My personal C coding style as of late 2023 by thetic on Monday October 09, 2023 @12:22AM (#37816906)
If only there were a way to indicate the function argument isn't mutated. </s>

My spidey senses tingle whenever I see const-ness cast away because it almost always means something is wrong. Either a function is missing a qualifier on an argument, or something very unsafe is happening. Why force callers to cast away const-ness in hopes that everything will be fine when you can just write the correct function signature.