This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

State machine in C problem

Dear all experts

I have to implement a state machine on my project using switch case statement look like this.

//-----------------------------------------
switch(state) {
    case STATE1:
    ...
    state=STATE2;
    break;
  case STATE2:
    ...
    state=STATE4;
    break;
  case STATE3:
    ...
    state=STATE1;
    break;
  case STATE4:
    ...
    state=STATE3;
    break;
}


Later I would like to add some conditions to change direction of the way it goes by omitting 'break' and adding some 'if' statements to take effect, then the code will look like this.

//-----------------------------------------
switch(state) {
  case STATE1:
    ...
if(condition 1) {
    state=STATE2;
  case STATE2:
}
    ...
    state=STATE4;
  case STATE3:
    ...
if(condition 2) {
    state=STATE1;
  case STATE4:
}
    ...
    state=STATE3;
}


If any of you have ever coded up a state machine in C before. Can you suggest me whether it will given a result in compilation error or run time error or not?

Another question is how is the different between the state machine using function pointer and this ordinary switch case statement in term of performance, amount of code, access time latency...etc? : )

Many thanks.

Parents
  • "Can you suggest me whether it will given a result in compilation error or run time error or not?"

    It will not result in a compilation error. The switch/case construct is a bit like a programmable goto. Using it like you have proposed is precisely what goes on "under the covers" in some state machine implementations and, in fact, is exemplified by the likes of FreeRTOS (using coroutines) and Protothreads.

    As for the run-time error aspect of it all, the switch/case combined with if/else, looping constructs, or anything else for that matter, has no run time error reporting mechanism. Any errors would be entirely yours to make.

    "Another question is how is the different between the state machine using function pointer and this ordinary switch case statement in term of performance, amount of code, access time latency...etc?"

    That, I think, is best for you to prototype and measure for yourself in a setting relevant to your target objective.

Reply
  • "Can you suggest me whether it will given a result in compilation error or run time error or not?"

    It will not result in a compilation error. The switch/case construct is a bit like a programmable goto. Using it like you have proposed is precisely what goes on "under the covers" in some state machine implementations and, in fact, is exemplified by the likes of FreeRTOS (using coroutines) and Protothreads.

    As for the run-time error aspect of it all, the switch/case combined with if/else, looping constructs, or anything else for that matter, has no run time error reporting mechanism. Any errors would be entirely yours to make.

    "Another question is how is the different between the state machine using function pointer and this ordinary switch case statement in term of performance, amount of code, access time latency...etc?"

    That, I think, is best for you to prototype and measure for yourself in a setting relevant to your target objective.

Children