2 ms·
Const is mildly useful but doesn't do a whole lot. It's as much of a reminder to the programmer as anything. Volatile's behavior is not really defined by the s
by mikeash 11y ago
Const is mildly useful but doesn't do a whole lot. It's as much of a reminder to the programmer as anything.
Volatile's behavior is not really defined by the standard and is rarely useful in any case. I'm pretty sure that you can ignore it and remain a fully compliant C compiler.
- Sir_Cmpwn 11y agovolatile is mostly useful for telling the compiler not to optimize out a variable. For example, if you use memory-mapped I/O, you can mark a pointer to it as volatile to tell the compiler that the hardware will be changing it and it shouldn't make any assumptions about that while optimizing. That's lower level than most people go, though, so I understand that it's not useful for the average C programmer.
- andrewchambers 11y agoWell, they wrote the plan9 kernel using this compiler, it can deal with IO fine.
- plorkyeran 11y agoPlan 9's compiler does not have a very sophisticated optimizer (which is part of why it's so fast), so I suspect that they simply didn't implement anything which would have made volatile necessary.
- mikeash 11y agoRight, if every assignment writes to memory and every expression reads from memory then you've effectively made every variable volatile (according to how compilers typically interpret volatile), so ignoring the qualifier wouldn't hurt anything.