3 ms·
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
by glitchc 3y ago
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.
- kibibu 3y agoShould perhaps be int Foo_bar(const Foo * self);
- glitchc 3y agoEven 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".
- jenadine 3y ago`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`
- PennRobotics 3y agoFor anyone thinking, "wtf const pointer order??" fall back on the spiral rule: https://c-faq.com/decl/spiral.anderson.html https://c-faq.com/decl/spiral.anderson.html
- jpcfl 3y ago> 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.