8 Replies Latest reply on Jun 5, 2018 2:04 AM by anboc_1467431

    Global vs Local Variables

    JoMe_264151

      Having recently discussed the use of global and local variables with Dana, I could not help but writing a bit of explanation for those who are not so experienced in the matter. Have a look at the attached document.

         

       

         

      Happy new year for all you forum supporters

         

      Bob

        • 1. Re: Global vs Local Variables
          RobynW_26

           Thank you so much!  -Robyn

          • 2. Re: Global vs Local Variables
            DaKn_263916

            Bob and I discussed pros and cons of Globals.

               

             

               

            I use in some programs extensively globals, and use pointers

               

            to them. Pariculary in cases where code space is challenged,

               

            or execution speed. You define them once, they do not get pushed/

               

            pulled on stack operations, just the pointers.

               

             

               

            Note however this works great in reducing code size where native

               

            machine size is 8 bit, and you have many ints and longs, especially

               

            longs.

               

             

               

            I think collectively we should do a sticky Globals vs Locals thread,

               

            get everyones input, and put it in an ap note.

               

             

               

            Regards, Dana.

            • 3. Re: Global vs Local Variables
              EmDr_1490911

              I've have experienced volatile memory data corruption (SRAM) on PSoC1 devices (which are programmed using the PSoC Developer IDE)  when using local variables on complex code applications (I'm currently developing a Built In Self Test program for this kind of chips as one of the goals of my PhD thesis). This is a serious issue that limits the application of good coding practices, and decoupling between functions.

               

              Best Regards.

               

              Emanuel Dri

              • 4. Re: Global vs Local Variables
                JoMe_264151
                AFAIK is "volatile" for a local variable not a senseful selection because the variable cannot get altered from another program point (function or interrupt handler).

                Can you please post a complete project that reproduces the bug you've found?

                 

                Bob

                • 5. Re: Global vs Local Variables
                  anboc_1467431

                  Hi,

                  and what about using local variables signed as externals?


                  At the university class, my professor warned us from using local variables, defined into functions, because of the problem of the stack.

                   

                  I solved this problem by using global variables, defining them where they belongs to, and making them accessible also in some *.c file, defining them as extern.

                   

                  Could it be marked as a good programming technique or not?

                   

                  Bests!

                   

                  Andrea

                  1 of 1 people found this helpful
                  • 6. Re: Global vs Local Variables
                    JoMe_264151

                    You may bust your stack by having too many local variables in use. This is (or should be) a common check at the end of a project to verify stack headroom. But nowadays the amount of sram available rarely limits the stack size.

                    For the sake of readability I (personally) group all external variables together and mark (as comment) where they are defined.

                    Instead of local variables on the stack you may use static variables.

                     

                    Bob

                    • 7. Re: Global vs Local Variables
                      EmDr_1490911

                      No, this is a compiler limitation

                      • 8. Re: Global vs Local Variables
                        anboc_1467431

                        What precisely?