How do you design your code to prevent it slipping into an undefined state, i thought all that was needed was to create a default to first state in case the program slips outside the defined area.
Writing a program to default to a given state when things go wrong is like building a road with guardrails on corners -- it keeps you from getting into deeper trouble, but it doesn't prevent the original screw-up. Careful coding, or using language constructs that don't allow an illegal state to be assigned, are the only things that will keep things from screwing up in the first place.
I suggest you follow up this or Mr. Smith's post with a post that tells what you're trying to do and what's screwing up. It's most likely that you're either explicitly writing the wrong value to your state someplace, or that you've got a pointer wrong and you're writing some perfectly valid piece of data to a horribly wrong spot.
Undefined states are to be avoided at almost all cost. This would result in a non-deterministic behaviour. Therefore : statemachines have to define all states. Overflows in addition/subtraction and multiplications have to be trapped or avoided. A division by zero has also to be trapped or avoided. In the embedded world, there is command.com that takes charge - usually. The watchdog shall be quiet.
Never thought about it. A finite state machine is often a machine that's trivial to code for, where some code contains thousands or millions of states, and the correctness of all combinations of states is not an interesting question, or even humanly possible to analyze.
One state might be a system call failure that is not accounted for, and the code crashes or malfunctions. The obvious prevention for such is to check the success and not assume. The pitfall here was a branch, not in the algorithm, but in the machine, that was not recognized as such.
ElectronDepot website is not affiliated with any of the manufacturers or service providers discussed here.
All logos and trade names are the property of their respective owners.