Many low level programs use the volatile keyword for types for memory mapping and such, however I'm sort of confused as to what it REALLY does in the background. In other words, what does it mean when the compiler doesn't "optimize away" the memory address?
Volatile Variables – What Does Declaring One Mean?
clow-level
Related Solutions
In general, the lower level the language, the more rigorous you have to be about style. Many of the same style rules to any language are important. Keep functions short. Keep like methods together. Generalize repeated code. Also, lower level code demands more comments as the code is itself less readable.
In most cases, the methods you'd use to keep things organized are more verbose and more manual. Basically, in C, you get something like encapsulation by keeping related functions in a single source file and using static globals for any data they need to share. You can use this to simulate private data in a class. Or more correctly, this creates something like a "Module", which was the structured programming world's answer to large scale program development before OO came along.
In a similar manner, you can use the same handle method used by system libraries like fopen
to create something like a class. Internally you have a table that maps handles to the "instance data". Or you can also pass a raw pointer back and forth.
In general, try to be as "functional" as you can. Avoid globals and side effects and do as much communication as you can with parameter passing.
One big problem is that you have no help with memory management, so you need to be very rigorous about when you allocate and free memory. Allocated data that goes together should be tied together somehow so that it can all be freed in a single destroy
function. Try to have a general structure to when things are allocated and freed. Also, allocate on the stack whenever you possibly can. One big help is the new dynamic arrays in C99.
The other big problem is the complete lack of collection classes. Best way to solve this is to go looking for third party solutions. You don't want to spend all of your time debugging custom hash table implementations.
You can go all out and fully implement OO by doing by hand what C++ compilers do for you. Make classes by creating structs with function pointers for virtual methods. You can use the same hack C++ does to derive classes by having the "child" class have function pointers to the base class methods in the same order in memory. This is a bad idea for a real program, but can be an interesting learning exercise.
Doesn't any mutable thing really manipulate state?
Yes.
And what does the "you should have to deal with little state" mean?
It means that less state is better than more state. More state tends to introduce more complexity.
In an impure language like C++, isn't state management really what you are doing?
Yes.
What are other ways to "deal with little state" other than limiting variable lifetime?
Minimize the number of variables. Isolate code that manipulates some state into a self-contained unit so that other code sections can ignore it.
Best Answer
volatile
means some other processor or I/O device or something can change the variable out from under you.With an ordinary variable, your program's steps are the only thing that will change it. So for instance if you read
5
from a variable and you don't change it, it'll still contain5
. Since you can rely on that, your program doesn't have to take the time to read the variable again next time you want to use it. The C++ compiler is smart to generate code that just remembers the5
.But you could read it as
5
, then perhaps the system loads data from disk into that memory, changing it to500
. If you want your program to read the fresh value500
, you need the compiler to not be too smart about using the previously-read5
. You need to tell it to reload the value every time. That's whatvolatile
does.An analogy for 5-year olds
Let's say you put a big sheet of paper on a table. In one corner of the paper, you write down the current score of an ongoing game,
3 to 4
. Then you go to the opposite side of the table and start writing a story about the game. Your friend who's watching the game updates the score in that corner as the game proceeds. She erases3 to 4
and writes3 to 5
.When you go to put the game score into your story, you could either:
3 to 4
, merrily assuming it didn't change (or not minding if it did), or3 to 5
now), and walk back. That's how avolatile
variable acts.