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

method to avoid global variables

I want to know method, How to avoid global variable in c microcontroller programing? or method to make global variable invisible.
Is there any perticular functions in built in c library?, that could be supportive to avoid global variables or hide global variables.

Parents
  • I just recall an incident.
    I worked bnriefly at a place thet had the "under penalty of severe bodily harm, do not use any global variables" rule.
    I had to make a variable accessible to 2 functions (b and y) and was told to do it this way:

    void main (void)
    {
    struct st
    ...
    unsigned char ch
    ...
    
    
    funca (*st)
    
    funcv( *st)
    
    ...
    }
    
    funca (*structure)
    {
    ...
    funcb (*structure)
    ...
    }
    
    funcb (*structure)
    {
    ....
    ralph = st.ch
    ...
    }
    
    funcv (*structure)
    {
    ...
    funcw (*structure)
    ...
    }
    
    funcw (*structure)
    {
    ...
    funcx (*structure)
    ...
    }
    
    
    funcx (*structure)
    {
    ...
    funcy (*structure)
    ...
    }
    
    funcy (*structure)
    {
    ....
    st.ch = george
    ...
    }

    with EVERY function that carries the *function able to modify ch where is the gain.

    this should look like
    
    unsigned char ch
    void main (void)
    {
    
    funca ()
    
    funcv()
    
    ...
    }
    
    funca ()
    {
    ...
    funcb ()
    ...
    }
    
    funcb ()
    {
    ....
    ralph = ch
    ...
    }
    
    funcv ()
    {
    ...
    funcw ()
    ...
    }
    
    funcw ()
    {
    ...
    funcx ()
    ...
    }
    
    
    funcx ()
    {
    ...
    funcy ()
    ...
    }
    
    funcy ()
    {
    ....
    ch = george
    ...
    }

    and run 47 times faster

    Erik

Reply
  • I just recall an incident.
    I worked bnriefly at a place thet had the "under penalty of severe bodily harm, do not use any global variables" rule.
    I had to make a variable accessible to 2 functions (b and y) and was told to do it this way:

    void main (void)
    {
    struct st
    ...
    unsigned char ch
    ...
    
    
    funca (*st)
    
    funcv( *st)
    
    ...
    }
    
    funca (*structure)
    {
    ...
    funcb (*structure)
    ...
    }
    
    funcb (*structure)
    {
    ....
    ralph = st.ch
    ...
    }
    
    funcv (*structure)
    {
    ...
    funcw (*structure)
    ...
    }
    
    funcw (*structure)
    {
    ...
    funcx (*structure)
    ...
    }
    
    
    funcx (*structure)
    {
    ...
    funcy (*structure)
    ...
    }
    
    funcy (*structure)
    {
    ....
    st.ch = george
    ...
    }

    with EVERY function that carries the *function able to modify ch where is the gain.

    this should look like
    
    unsigned char ch
    void main (void)
    {
    
    funca ()
    
    funcv()
    
    ...
    }
    
    funca ()
    {
    ...
    funcb ()
    ...
    }
    
    funcb ()
    {
    ....
    ralph = ch
    ...
    }
    
    funcv ()
    {
    ...
    funcw ()
    ...
    }
    
    funcw ()
    {
    ...
    funcx ()
    ...
    }
    
    
    funcx ()
    {
    ...
    funcy ()
    ...
    }
    
    funcy ()
    {
    ....
    ch = george
    ...
    }

    and run 47 times faster

    Erik

Children
No data