FSM functor help

This is a discussion on FSM functor help within the C++ Programming forums, part of the General Programming Boards category; I just need someone to help me point out my array = newstate pointer mistake... Code: #include <iostream> #include <cstddef> ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Location
    New York
    Posts
    123

    FSM functor help

    I just need someone to help me point out my array = newstate pointer mistake...

    Code:
    #include <iostream>
    #include <cstddef>
    using namespace std;
    
    typedef void (*state_t)( char * inData, unsigned int , size_t );
    
    class State
    {
      state_t Curstate; 
      unsigned int label; 
      
      public:
      
      State(void)
     {  Curstate = NULL; }
    
      State( state_t & nState , unsigned int nID)
     {
           if( nState != NULL )
          {
              Curstate = nState;
              label = nID;
          }
          
      }
    
     void operation( state_t & nState , unsigned int nID)
     {
           if( nState != NULL )
          {
              Curstate = nState;
              label = nID;
          }
          
      }
    
     void operation ( char *& Data , size_t Size )
     {
         Curstate( Data ,label, Size );
     }
    
     
     unsigned int pass_ID(void) { return label; }
    
     };
    
    class FSM 
    {
      State state;
      
      char * data;
    
      state_t  * array; 
    
      unsigned int ID;
      
      public :
    
      FSM( state_t * newstate  )
     {
       array = newstate;
       ID = 0x0000000A;
       data = NULL;
       state( *array, ID); 
     }
    
    void doAction(void)
    {
    
       state( data , 4);
    
      if( ID ^ state.pass_ID() )
     {
       ++i;
       state(array[ ID % 10], ID );
     }
    
    };
       
    }
    
     
     void process1( char * data , unsigned int ID , size_t Size)
    {
      if( data = NULL )
       data = new char [ Size];
      
      cout<<"this is your ID "<<ID<<" I will now let go to next function"<<endl;
    
      //passcode
      *data = "hi";
    
     ++ID;
    }
    
     void process2( char * data, unsigned int ID , size_t Size )
    {
      cout<<"the passcode is "<<data<<endl;
    
      *data = "no";
    }
    
    int main(void)
    {
      state_t state[2];
     
      state[0] =&process1;
     
      state[1] =&process2;
    
      return 0;
    }
    Last edited by Darkinyuasha1; 12-15-2011 at 04:45 AM. Reason: This was a test functor just to see if it could replace my virtual function state machine

  2. #2
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,656
    Code:
    if( data = NULL )
    That's an assignment, not a comparison.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,014
    >>*data = "hi";
    Never going to work. *data is a char, "hi" is a const char[3].
    Furthermore, you are getting a memory leak because you don't clean up.
    Use std::string!

    And the value of ID is not saved after the function. Use a reference!

    >>*data = "no";
    Same problem here as above.

    >> State state;
    >> state( *array, ID);
    You are trying to use a variable as if you are calling a function.

    >>if( ID ^ state.pass_ID() )
    Did you really mean to use XOR here?

    >>NULL
    Use nullptr instead.

    Lastly, indent properly.
    Last edited by Elysia; 12-15-2011 at 05:50 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Oct 2006
    Location
    New York
    Posts
    123
    Does visual C++ already have the nullptr idiom? eeh thanks for pointing out the flaws, but i already found them ..

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    2,053
    If it doesn't use 0. NULL is for C.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,014
    Quote Originally Posted by Darkinyuasha1 View Post
    Does visual C++ already have the nullptr idiom?...
    It does.

    Quote Originally Posted by King Mir View Post
    If it doesn't use 0. NULL is for C.
    NULL is for both C and C++.
    Whether or not you want to use NULL for C++ is another matter. There are both advantages and disadvantages to that.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Apr 2006
    Posts
    2,053
    Quote Originally Posted by Elysia View Post
    Whether or not you want to use NULL for C++ is another matter. There are both advantages and disadvantages to that.
    NULL is defined in C++, but it is a C idiom. It should not be used in C++.

    The short of it is, the disadvantages out-way the advantages. Using NULL in C++ is liable to obscure bugs.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,014
    Using 0 can cause confusion, as well. If you overloaded a function taking an int and one taking a pointer, 0 will call the int one, not the pointer. So how do we call the pointer function? It is likely this will cause just as much confusion as NULL.
    Unfortunately, there just aren't any good solutions, other than defining your own nullptr type (which C++11 finally have fixed).
    So while evil it may be, it is fine if you know what you're doing. So I call this one a preference.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Apr 2006
    Posts
    2,053
    NULL causes more confusion, because a programmer using it will not expect it to be treated like an int, and on most compilers it will be (because it's defined as 0). I agree that nullptr is the best option, where supported.

    Once such undesired conversion is identified, it can be fixed with an explicit cast to the appropriate pointer type.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help with a functor
    By nullspace in forum C++ Programming
    Replies: 1
    Last Post: 08-26-2011, 12:12 AM
  2. Functor errors
    By Dae in forum C++ Programming
    Replies: 2
    Last Post: 02-24-2011, 05:10 AM
  3. Lambda as argument to functor
    By StainedBlue in forum C++ Programming
    Replies: 1
    Last Post: 07-02-2010, 02:59 PM
  4. delete functor
    By KIBO in forum C++ Programming
    Replies: 2
    Last Post: 05-08-2009, 09:25 AM
  5. Map and Functor :: STL
    By kuphryn in forum C++ Programming
    Replies: 8
    Last Post: 11-26-2002, 12:09 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21