I see a Java programmer evolves into a C programmer
Programmer Humor
Welcome to Programmer Humor!
This is a place where you can post jokes, memes, humor, etc. related to programming!
For sharing awful code theres also Programming Horror.
Rules
- Keep content in english
- No advertisements
- Posts must be related to programming or programmer topics
Some people hate that C is dangerous, but personally I like its can-do attitude.
“Hey C, can I write over the main function at runtime?”
Sure, if you want to, just disable memory protection and memcpy whatever you want there! I trust you.
It’s a great attitude for a computer to have.
What is the context of the original image?
Could be simply a way to make sure the button never moves again. I would have simply taken out the knob, personally.
I've used it in the past when having flash memory blocks that could change but you need the compiler to put them into flash memory and not RAM. It's mainly to get the compiler to stop assuming that it can optimize using the default value.
When you set the port speed to no negotiate.
If you have a memory-mapped peripheral where there's a readonly register, I could see it being const volatile
.
It makes more sense if you think of const
as "read-only". Volatile just means the compiler can't make the assumption that the compiler is the only thing that can modify the variable. A const volatile
variable can return different results when read different times.
I thought of it more in terms of changing constants (by casting the const
away). AFAIK when it's not volatile
, the compiler can place it into read-only data segment or make it a part of some other data, etc. So, technically, changing a const volatile
would be less of a UB compared to changing a regular const
(?)
AFAIK when it’s not volatile, the compiler can place it into read-only data segment
True, but preventing that is merely a side effect of the volatile qualifier when applied to any random variable. The reason for volatile's existence is that some memory is changed by the underlying hardware, or by an external process, or by the act of accessing it.
The qualifier was a necessary addition to C in order to support such cases, which you might not encounter if you mainly deal with application code, but you'll see quite a bit in domains like hardware drivers and embedded systems.
A const volatile variable is simply one of these that doesn't accept explicit writes. A sensor output, for example.
const volatile is used a lot when doing HW programming. Const will prevent your code from editing it and volatile prevents the compiler from making assumptions. For example reading from a read only MMIO region. Hardware might change the value hence volatile but you can't because it's read only so marking it as const allows the compiler to catch it instead of allowing you to try and fail.
I will not tell my kids regular scary stories. I will tell them about embedded systems