Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6182735
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T01:15:01+00:00 2026-05-24T01:15:01+00:00

If I make a state machine and want to use an interface like this:

  • 0

If I make a state machine and want to use an interface like this:

AddState ( state1, state2, Key_UP );
AddEvent ( Key_UP );
AddEventFunction ( Key_UP, &UP_Function);
AddStateFunction ( state1, &State1_In_Function, &State1_Out_Function);
AddStateFunction ( state2, &State2_In_Function, &State2_Out_Function);

State1_In_Function  ( void ) { printf ( "In #1 \n" ); }
State1_Out_Function ( void ) { printf ( "Out #1 \n" ); }
State2_In_Function  ( void ) { printf ( "In #2 \n" ); }
State2_Out_Function ( void ) { printf ( "Out #2 \n" ); }
UP_Function         ( void ) { printf ( "Goin UP \n" ); }

That way when I am in state1 and the FSM receives Key_UP the program prints:

Out #1
Goin UP
In #2

The question is how to store the state and transitional information inside the class without the programmer being required to change an array size. I was thinking I could use a 2D array and make it a state table like usual and to make it more portable I would just handle the addition of events and states by using the vector type to resize as needed. The problem with vectors is that not many embedded devices can use the memory allocation calls. My second option is to call a constructor with the state machine and pass it the size the table would require to be but then if I add any new states or events I need to change these values as well…

So how should I store my states, events and function pointers?!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T01:15:02+00:00Added an answer on May 24, 2026 at 1:15 am

    You could simply store them on the stack, though it’s a bit more difficult 🙂

    Still, it’s an amusing solution, so here you go. The underlying principle is to play with decorators and mutability. Example of code:

    State state1, state2; // creates a state
    Event KEY_UP;
    Event KEY_DOWN;
    
    Transition t0(state1, KEY_UP, state2);
    Transition t1(state2, KEY_DOWN, state1);
    

    How does it work ?

    Instead of state1 being a “simple” object, it will be slightly more convoluted. Something like:

    struct State;
    
    struct StateImpl {
      StateImpl(char const* n): name(n) {}
      char const* name;
    };
    
    struct StateNode {
      StateNode(Event e, State const& s, StateNode const* n):
        event(e), state(s), next(n) {}
    
      Event event;
      State const& destination;
      StateNode const* next;
    };
    
    struct State {
      State(char const* name):
        me(0), impl(name) {}
    
      StateNode const* me;
      StateImpl impl;
    };
    

    And then we define a Transition:

    struct Transition {
      Transition(State& origin, Event e, State const& destination):
        node(e, destination, origin.me)
      {
        origin.me = node;
      }
      StateNode node;
    };
    

    Informally, we are building a singly-linked list, with the head sitting in State. We update the head each time we add a transition.

    Upon an event occurrence, one needs to walk this list until either the event is encountered, and thus suitably dispatched, or the null pointer is reached, indicating that the event shall not have been received in this state.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have tried to make a webservice interface to a state-holding COM component. The
I want to model a kind of FSM(Finite State Machine). I have a sequence
i made a state machine and would like it to take advantage of generics
In C++ I'm trying to make a simple state machine for a game, based
I'm trying to make a function that holds state but is called with foo().
VB6 IDE can get into a state when the Make XXXX menu option under
I would (as the question states) like to make an asynchronous call, preferably using
I'm trying to make a simple C# web server that, at this stage, you
I would like to implement a FSM/pushdown automaton parser for this syntax: parser with
I have to draw a small finite state machine that has some reflexive transitions

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.