5 ms·
> For example, D enables a "pointer to immutable data" type, while C does not. Wait, isn’t that what const int *x; does? I.e. a pointer to a constant.
by teddyh 6y ago
> For example, D enables a "pointer to immutable data" type, while C does not.
Wait, isn’t that what
const int *x;
does? I.e. a pointer to a constant.
- WalterBright 6y agoIn C, the value pointed to by `x` cannot be changed via a write through `x`, but if there is another pointer to that value, it can be changed through that. In D, `immutable(int)* x` cannot be changed by any reference to the value.
- teddyh 6y agoYou’d think that C could do any appropriate optimizations as well as D, since the C compiler (in theory) should know whether there can exist (in any given program) any writeable pointers to the same value or if there only can exist pointers to const.
- innocenat 6y agoPointer arithmetic would surely make all value mutable. Or even simple array access.
- marton78 6y agoOnly inside the same compilation unit.
- ygra 6y agoDoing it across the whole compilation requires LTCG, but that does exist as well. It just makes incremental builds a pain with large binaries.
- knome 6y agoconst int world = 42; const int const * const hello = &world ; Apparently, you can be very const and `gcc -Wall -Wextra -std=c99` won't raise any complaints.
- teddyh 6y agoFirstly, isn’t that a syntax error? There’s a stray “const” in there. You probably meant const int * const hello = &world; Secondly, what should the compiler complain about? You have a const int, and then a const pointer to const int, pointing to that first const int. What’s the problem? Thirdly, the latest C version supported by GCC is “-std=c17”.
- tick_tock_tick 6y agoNah const is fun int main() { const int world = 42; const const const int const const * const const hello = &world; return 0; } Is a valid program https://onlinegdb.com/SJcwJrRzd https://onlinegdb.com/SJcwJrRzd
- teddyh 6y agoSure, but those extra “const” do not mean anything, and are apparently ignored by the compiler.
- flohofwoe 6y agoIsn't that what 'restrict' is for? It's a new type of footgun of course because the compiler doesn't detect if the value is actually accessed through another pointer, but by simply ignoring that possibility via 'restrict', the compiler should have the same optimization opportunities, no?
- WalterBright 6y ago> but by simply ignoring that possibility via 'restrict', If you deceive the compiler like that, it is entitled to hand you a broken executable :-/ Besides, I forgot to mention that D immutable data is inherently thread safe, no synchronization required. And you can have as many live references to it as you like. Immutable data is part of D's support for functional programming.
- flohofwoe 6y agoWell yeah, sure. I wasn't picking on D, just pointing out that C offers an escape hatch, no matter how dangerous that might be ;)
- yvdriess 6y agoI haven't found the compiler ever optimizing something different because of constness. The various escape hatches means that it is really hard for the optimizing compiler to make certain assumptions, especially if it's connected to something with external linkage. A function with a const struct arg that you never take the address of? compiler emits memcpy!
- Omin 6y agorestrict is so rarely used that clang has had open code generation bugs for years. Rust would like to use its non-aliasing guarantees to generate better code, but clang is unable to reliably generate correct code. So even if you manage to reason correctly around `restrict` in C, you can't count on the compiler to translate your code correctly. GCC also had bugs around restrict, but I don't know about their current status.
- 6y ago
- jcelerier 6y ago> In D, `immutable(int)* x` cannot be changed by any reference to the value. even if I use a magnetic needle to change the bits in my RAM ? :)
- petertodd 6y agoThere's an easy answer to that: using a magnetic needle to change the bits in your RAM violated the assumptions of the compiler, leading to undefined behaviour. What exactly will happen in that circumstance is just that: undefined.
- MaxBarraclough 6y agoTo put that another way: hardware failures are beyond the scope of the compiler.
- wizzwizz4 6y agoMaybe your compiler.