Global vs Local Variables

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
lock attach
Attachments are accessible only for community members.
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

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

0 Likes
8 Replies
RobynW
Employee
Employee
5 likes given First comment on blog 10 likes received

 Thank you so much!  -Robyn

0 Likes
ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given

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.

0 Likes
EmDr_1490911
Level 2
Level 2
First like received 5 questions asked 5 replies posted

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

0 Likes
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

0 Likes
anboc_1467431
Level 3
Level 3
First like received

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

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

0 Likes

No, this is a compiler limitation

0 Likes

What precisely?

0 Likes