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 don't know if I've understood you correctly, but you might consider these options:

    1) Use a static local variable within a function. It will retain its value between calls to that function but be 'invisible' to the rest of the program.

    2) Use a static variable at file scope (ie declared outwith all functions in the source file). It will be 'invisible' outside that source file.

Reply
  • I don't know if I've understood you correctly, but you might consider these options:

    1) Use a static local variable within a function. It will retain its value between calls to that function but be 'invisible' to the rest of the program.

    2) Use a static variable at file scope (ie declared outwith all functions in the source file). It will be 'invisible' outside that source file.

Children
  • How to avoid global variable

    So you were taught by a real Cidiot.

    WHAT THE #{!! IS WRONG WITH GLOBAL VARIABLES?

    Erik

  • "WHAT THE #{!! IS WRONG WITH GLOBAL VARIABLES?"

    Find out for yourself: take a largish project and make all the variables global, then report back with your findings.

  • Global variables are a perfectly valid way to communicate results from one part of the application to another. Presumably, what you want to do limit the scope of such variables so that they CANNOT be accessed by parts of the application that have no business accessing them.

    I hope that you already have source code that is divided into modules and perhaps you have subsystems within the application that comprise a number of modules - these mutually communicate, but these interfaces should be hidder from other subsystems.

    Now, for each subsystem you need to have variables that can only be accessed by modules withing the subsystem. To do this create files such as:

        subsystem_name_data.c
        subsystem_name_data.h
    
    Put all your subsystem common variables in these files. Modules that are a part of the subsystem should include the xxx_data.h file. You will need to enforce a rule to the effect that modules outside the subsystem may not include xxx_data.h.

    Each subsystem will require an interface to other subsystems within the application. To do this create files such as:
        subsystem_name_api.c
        subsystem_name_api.h
    
    Put your accessible variables and function names in these files. You will need to enforce the rule that only modules in subsystems that have business communicating with this subsystem are allowed to include the xxx_api.h file.

    Finally, you might need to have things that are actually globally defined. To do this, create files such as:
        system_name_data.c
        system_name_data.h
    
    Put your handful of global definitions in these files. Any module that requires the global defintions can include the xxx_data.h file.

    Futher refinements are possible, such as sub-subsystems, but this should give you a good start.

    There is no way to automatically enforce the include rules - you will have to check manually.

  • Find out for yourself: take a largish project and make all the variables global, then report back with your findings.

    Who said anything about all the variables ?

    I do, of course, use lots of local variables, what I reacted to is the idiotic C thing that some teach "under penalty of severe bodily harm, do not use any global variables".

    How would you communicate smartly between an ISR and the main without a global variable.

    Erik

  • 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

  • "Who said anything about all the variables ?"

    I think you know who it was: me.

    "I do, of course, use lots of local variables"

    Ok, ask yourself why you made those variables local rather than global.

    "what I reacted to is the idiotic C thing that some teach "under penalty of severe bodily harm, do not use any global variables""

    Who said anything about not using any global variables?

    "How would you communicate smartly between an ISR and the main without a global variable."

    Oh, that's easy. Loop back some port pins.

  • Who said anything about not using any global variables?
    the OP

    Erik

  • "Who said anything about not using any global variables?
    the OP"

    No he didn't:

    "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."

    I don't see the words 'all' or the phrase 'not using any' in there.

  • http://www.bartleby.com/61/96/A0549600.html
    defines avoid as To stay clear of; shun.

    That means all

    Erik

  • "defines avoid as To stay clear of; shun.

    That means all"

    So you think that these two phrases are semantically identical?

    "Avoid global variables"
    "Avoid all global variables"

    I think your grasp of the language is better than that.