3

I was reading http://www.netrino.com/Embedded-Systems/How-To/State-Machines-Event-Driven-Systems later in the article they provide implementation of the small FSM in C language.

I don't quite understand why they chose function pointers. In my understanding pointers to functions are useful when one needs the same interface, but for different types of "events", for example parsing some Internet protocol packet (it's convenient to register one pointer to function and assign to it different functions, one to parse HTTP, second to parse FTP and so on. This is merely an example, but I think you got my point).

But this is not what I see in the article, IMHO for the state machine astraightforward implementation would suffice, or may be I'm wrong?

unwind
  • 391,730
  • 64
  • 469
  • 606
Mark
  • 1,751
  • 3
  • 14
  • 14

3 Answers3

4

The most straightforward way to implement an FSM is via function pointers.
In FSM a mapping is defined that associates an event and a state to a specific behavior.
So depending on the state, the same event should be treated differently. The usage of function pointers is the most appropriate for this.
Additionally it is easily extensible. To add new behavior, you just add new functions and update the mapping table for the new behavior.
Also it can be extended to have multiple statemachines.
I guess there could be other "hacks" to implement an FSM but function pointers is standardized approach.

Cratylus
  • 52,998
  • 69
  • 209
  • 339
3

I'll admit, that article was a bit tl;dr for me. But I will say one of the easiest and most elegant ways I've seen to create finite state machines in C is from this post

Here is a snippit

#define FSM
#define STATE(x)      s_##x :
#define NEXTSTATE(x)  goto s_##x

FSM {
  STATE(x) {
    ...
    NEXTSTATE(y);
  }

  STATE(y) {
    ...
    if (x == 0) 
      NEXTSTATE(y);
    else 
      NEXTSTATE(x);
  }
}

Some people may balk at the use of goto in any circumstance, but I think this is one implementation which really uses it quite beautifully.

So to answer your question, yes, not only do I believe that function pointers may be overkill for a FSM, but it also tends to obfuscate the code.

Community
  • 1
  • 1
SiegeX
  • 135,741
  • 24
  • 144
  • 154
2

First, have a look to this this answer, that is (in my opinion) easier to understand than the one you posted.

Second, you are right: function pointers are useful to implement different behavior for the same "event pattern". This is what is normally called Polymorphism in OOP (have a look to this wikipedia article).

So, if you think about it... this is exactly what you need for a FSM: to react in different ways to the same event, that is: to implement a different function for each state transition in your FSM.

At a first glance, you could say that a "classic" switch statement is enough to implement this, but the flexibility and the opportunity to extend the function-pointer implementation will reward you in the long term.

Community
  • 1
  • 1
Nova
  • 321
  • 9
  • 20