Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Q. What's a good prefix for global variable?

Status
Not open for further replies.
It's down to both end use of the software and style..

For team projects and business software, where there could be conflicts between use or unwanted visibility of data, globals are a real bad idea.


For real-time code, embedded, single-author games etc. those problems do not apply and using globals or static storage as appropriate is rather more efficient than unnecessarily passing data or pointers.

Also, some of the workarounds that people come up with to avoid actual globals just disguise them rather than eliminating them - eg. a shared object to allow common data such as status or config info to be accessed by multiple other code modules compiles to the same code as basic global variables would...
 
Pommie .. I read this thread several times and now I realise what a poor 'programmer' I must be... my 'C' if full to the brim with global variables.. Can't seem to do without them ! A prefix sounds like a good idea and will start using one ...
 
I kinda understand the principal of "no globals" when it comes to large team projects. However, most of my projects will only have myself writing code so i'm more comfortable with globals. I do try to reduce my use as much as reasonable as they do eat ram but having function calls just to read/write a private variable eats up program space. Hence, when it comes to \[ \mu \]controllers I tend to use them as needed.

Mike.
Edit, it seems using the character function under latex turns it into a complete paragraph rather than a character!!! I just wanted to write microcontroller with a mu character!!!
 
You cheated and used charmap or something.:p

Go on, do it via the tools available.

Mike.
Edit, µ - see I can do it too. Alt 0181.
 
I kinda understand the principal of "no globals" when it comes to large team projects. However, most of my projects will only have myself writing code so i'm more comfortable with globals. I do try to reduce my use as much as reasonable as they do eat ram but having function calls just to read/write a private variable eats up program space. Hence, when it comes to \[ \mu \]controllers I tend to use them as needed.

I agree, I love global variables - when you've not got 'unlimited' resources (such as in a PIC) then it makes absolute sense to use them.

For example, in my current GPRS project(s) I have a number of global variables that need to be accessed from various subroutines as they store the settings for the operation of the unit - such as the time and/or interval that it's triggered. Passing them as local variables to subroutines would be much more inefficient, and wasteful of resources.

As for globals 'eating ram', I would disagree, as they mean that you know what you're using, rather than hoping the compiler can handle them all in a reliable manner. Using globals means one copy of the variable, at a location that you know.

Obviously I do pass variables (or pointers to them), where it makes more sense to do so.
 
Did you insert as 'inline' or 'block'?
 
How about g_

(g underscore)

A quick look will allow you to realize that it's a global variable.

Recently, I spent some time programming in c#. No global variables allowed at all. Sometimes, they make things so easy.
Unless they're being shared by multiple threads, in which case you have to be sure to use a locking construct (mutex)

Microcontrollers are a different animal. You're not running more than one thread, so there's no risk.

But you have to be careful if the same global variable is being accessed from more than one interrupt.

(Break out the atomic locking gizmo... :D )
 
Last edited:
How about g_

(g underscore)

A quick look will allow you to realize that it's a global variable.

Recently, I spent some time programming in c#. No global variables allowed at all. Sometimes, they make things so easy.
Unless they're being shared by multiple threads, in which case you have to be sure to use a locking construct (mutex)

Microcontrollers are a different animal. You're not running more than one thread, so there's no risk.

But you have to be careful if the same global variable is being accessed from more than one interrupt.

(Break out the atomic locking gizmo... :D )
But then if you need a g_string local variable you are screwed
 
I would resort to
GLBL_

I personally prefer names not too cryptic, if at all possible, somewhat legible.

Jointly with good comments, helped me when revisiting old code.
 
The local university's style guide says, avoid them whenever possible but, if you cannot figure out how to avoid them, use prefix "g_" as in g_variable
 
This is a bit of a joke with OO programmers that work in large teams. The idea being that any global variable should start with // therefore making it a remark. It works for those kinds of projects but for the stuff I do, mainly microcontroller stuff, they can be very useful and sometimes necessary to increase speed and reduce program usage. I guess when you're working on a multi GHz processor then speed is not a worry.

Mike.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top