Re:My personal C coding style as of late 2023
by glitchc on Sunday October 08, 2023 @10:56PM
(#37816485)
I'm afraid you are mistaken. In particular for pointers, const does not guarantee that the memory at the location pointed to won't change. Const only guarantees that the address itself doesn't change.
[ Parent
| on HN ]
Re:My personal C coding style as of late 2023
by kibibu on Sunday October 08, 2023 @11:08PM
(#37816534)
Should perhaps be
int Foo_bar(const Foo * self);
Re:My personal C coding style as of late 2023
by glitchc on Sunday October 08, 2023 @11:24PM
(#37816615)
Even then, some other function can change the memory at the address of self while this one is executing, especially in concurrent systems. Additionally, any other pointer pointing to the same address can also modify self's memory. const in this case is really just "scout's honour".
Re:My personal C coding style as of late 2023
by jenadine on Monday October 09, 2023 @01:14AM
(#37817101)
`const Foo*` and `Foo const*` are exactly the same and just a question of style (east-const vs. west-const)
Not to be confused with `Foo *const`
Re:My personal C coding style as of late 2023
by PennRobotics on Monday October 09, 2023 @03:39AM
(#37817974)
For anyone thinking, "wtf const pointer order??" fall back on the spiral rule:
Re:My personal C coding style as of late 2023
by jpcfl on Monday October 09, 2023 @10:08AM
(#37820603)
> const does not guarantee that the memory at the location pointed to won't change
I didn't say this. I said a `const` function tells the reader that the state of an object doesn't change.
Another reader correctly pointed out that there are ways to modify the state of a `const` parameters (indirection and const cast), but I would argue that such an API is poorly-designed.
To qualify my original comment, a reader only knows a function doesn't change an object's state if the API is well-designed.