473,765 Members | 2,070 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

opinion wanted about global vars

Hi,

I am working on an application, which will run embedded without an OS.
The app is build up out of a couple of well defined parts. At first I
wanted to keep those parts seperated and use opaque data types to
transfer information in between them.
At some stage I was stuck and needed to make a variable global, and I
also needed to make the struct declaration public to some other parts.

Looking through the code I found out that lots of functions, which were
intended to alter the data structure would contain only one or two
lines. So I wrote macros to speed things up.
Looking through the assembly I discovered that the compiler would leave
some function calls completely out because of the optimisation of the
code including the macro.

So now for my real question, what is your opinion on using global
variables/structures and macro's to access them in this sort of environment?
I normally try to avoid global variables as much as possible, but in
this case I see somereal advantages esspecially over the opaque data
approach.

Mark

Nov 13 '05 #1
11 2565

"Capstar" <sp***@eg.homei p.net> wrote in message
news:3F******** ****@eg.homeip. net...
now for my real question, what is your opinion on using global
variables/structures and macro's to access them in this sort of environment? I normally try to avoid global variables as much as possible, but in
this case I see somereal advantages esspecially over the opaque data
approach.


(By global I mean a variable with indefinite scope and indefinite extent)
(Structured code is code that does not use any unconditional jumps and none
of the paths the code can take crosses over each other)

If the design turns out to be better, then use globals. Avoiding globals is
not
a goal in itself. The goal is to make better designs and more readable code.

It is the same thing with structured programming, it is not a goal in
itself.
If goto or break or continue makes your code better then you should use
them.
Not doing so is wrong. (or at least less good). I once read a comparison of
two functions representing the same abstraction, one using goto and one
without
goto. The conclusions was that the implementation was more or less
equivalent
because the function without goto was more complex and a bit harder to read
of
the two, the function with goto was bad because it used goto. That is just
idiotic.
Can't remember where that was though.

Situations where unstrucured code and globals are desireable are rare, but
not
unheard of. If you avoid them just on general principle then you are doing
something wrong. If you need globals, try to make them static, if that hurts
more
than it helps do not make them static.

Bad thing about globals is that there is a new potential side effect in all
functions that has access to that variable and that it might be more
difficult
to introduce threading in a program that uses them.

Just remember that avoiding goto and globals is not a goal in itself and
that
structured code is not a goal in itself.

Unless of course you are doing an academic exercise. (Not having a go at
those,
just saying that sometimes they become a goal in themselves).

I guess I am saying that the words "never" and "always" are always wrong in
software engineering. :p

--
Thomas, the pragmatist.
Nov 13 '05 #2
Capstar escribió:
I am working on an application, which will run embedded without an OS.


Then, why you post in a group about programming for a concrete OS?

Regards.
Nov 13 '05 #3
Julián Albo <JU********@ter ra.es> scribbled the following
on comp.lang.c:
Capstar escribió:
I am working on an application, which will run embedded without an OS.
Then, why you post in a group about programming for a concrete OS?


Why do YOU post in a group about programming, which does not have
anything to do with OSes?

--
/-- Joona Palaste (pa*****@cc.hel sinki.fi) ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste W++ B OP+ |
\----------------------------------------- Finland rules! ------------/
"The day Microsoft makes something that doesn't suck is probably the day they
start making vacuum cleaners."
- Ernst Jan Plugge
Nov 13 '05 #4
Capstar wrote:
I am working on an application, which will run embedded without an OS.
The application is build up out of a couple of well defined parts.
At first, I wanted to keep those parts separated
and use opaque data types to transfer information in between them.
At some stage, I was stuck and needed to make a variable global.
Really. Can you post some of the code
and explain to us why you thought you were "stuck"?
I also needed to make the struct declaration public
to some other parts.

Looking through the code I found out that lots of functions,
which were intended to alter the data structure
would contain only one or two lines.
So I wrote macros to speed things up.
Why didn't you use inline functions instead?
Looking through the assembly I discovered that
the compiler would leave some function calls completely out
because of the optimization of the code including the macro.

So now for my real question,
"What is your opinion on using global variables/structures and macro's
to access them in this sort of environment?"
I normally try to avoid global variables as much as possible
but, in this case, I see some real advantages
especially over the opaque data approach.


Global constants are good but you should avoid global variables.

Please post some simple examples so that
we can show you how to modify them to avoid the global variables
that you claim that you needed.
Nov 13 '05 #5
On Wed, 17 Sep 2003 14:56:23 UTC, Capstar <sp***@eg.homei p.net> wrote:
So now for my real question, what is your opinion on using global
variables/structures and macro's to access them in this sort of environment?
I normally try to avoid global variables as much as possible, but in
this case I see somereal advantages esspecially over the opaque data
approach.


Avoid globals wherever possible. But don't avoid global variables only
to avoid them!

In other words: Whenever possible concentrate all functionality to an
variable into one translation unit.

As this is not ever possible define a separate header that can be
included only in all the modules that need access to the variables and
another header that contains only the interfaces to the functions
designed to use from somewhere.

A translation unit that needs access to the internals of another
transation unit can include both header, the public one and the
internal one.

It lefts on you and your design to place the definitions on the
translation unit they belongs to and declarations (extern) on private
or public headers. As anything you publishes in an public header can
included anywhere it lefts on the discipline and the rules for the
whole team NOT to misuse this. But it is more save as to store
anything in one global header.

It lefts at least on you to find the right compromise.

--
Tschau/Bye
Herbert

eComStation 1.1 Deutsch Beta ist verügbar
Nov 13 '05 #6
On Wed, 17 Sep 2003 16:56:23 +0200, Capstar <sp***@eg.homei p.net>
wrote in comp.lang.c:
Hi,

I am working on an application, which will run embedded without an OS.
The app is build up out of a couple of well defined parts. At first I
wanted to keep those parts seperated and use opaque data types to
transfer information in between them.
At some stage I was stuck and needed to make a variable global, and I
also needed to make the struct declaration public to some other parts.

Looking through the code I found out that lots of functions, which were
intended to alter the data structure would contain only one or two
lines. So I wrote macros to speed things up.
Looking through the assembly I discovered that the compiler would leave
some function calls completely out because of the optimisation of the
code including the macro.

So now for my real question, what is your opinion on using global
variables/structures and macro's to access them in this sort of environment?
I normally try to avoid global variables as much as possible, but in
this case I see somereal advantages esspecially over the opaque data
approach.

Mark


I use the phrase "non local variables" (my invention) to refer to
variables defined at file scope, regardless of whether they have
internal or external linkage.

The biggest difficulties caused by the use of this type of variable
are, in my opinion, these two:

1. Accidental modification via name duplication.

2. Difficult to understand code due to such variables being modified
"behind the scenes" of code one is reading.

We have come up with rules for use of such objects in coding projects,
especially but not limited to those involving more than one
programmer.

Truly global variables are absolutely forbidden. Values which must be
used throughout a program need to belong to some specific code. If
the values must be read by a lot of different code, so much that "get"
calls become a burden, put them all in a structure and pass around a
const qualified pointer to it to functions that need the values.

If you have program-wide data that must be changed very often and from
all over the code, redesign right now.

The next step is to minimize the accessibility of all non-locals.
Modularize your code. When a variable needs to be shared between a
few related functions that fit comfortably in a single source file,
define it there with the static keyword.

Otherwise break the code into functional modules (example, file i/o,
printing, spell checking, whatever). Each module goes into its own
directory. Non-locals needed by various functions in different source
files within the modules is prefixed with a module specific prefix to
and declared in a header located in the module's directory, not
allowed to be included by source files outside the directory.

The module-specific prefix prevents global name provision, and
simulates multiple external namespaces such as the C++ namespace
feature provides. So you can have PR_lines_per_pa ge in your print
module that can't possible clash with anything defined in your file
i/o module, where everything starts with FIO_. This is useful for
functions with external linkage as well.

Finally the important restriction on non-local variables. With a few
trivial exceptions, we require the following:

If a function accesses the value of a non-local variable, that value
will remain invariant for each execution of that function other than
for visible modifications in that function.

This last one has saved a lot of grief. It initially came about
because of a very poorly coded state machine implementation, like
this:

enum state { STATE0, STATE1, STATE2, STATE3 } the_state;

void some_func(void)
{
if (STATE0 == state)
func_1();

if (STATE1 == state)
func_2();

if (STATE2 == state)
func_3();

if (STATE3 == state)
func_4();
}

When questioned in a code inspection as to why either a switch
statement or an if..else if...else if...else structure was not used,
the author revealed that some (but not all) of the called functions
might modify the state variable. In some cases, to more than one
possible value. So a single call of the function could result in
calling at least one, but sometimes two or three of the different
functions.

Hence the insistence on visible changes to non-locals if they changed
during the execution of a function.

Even something like this:

switch (state)
{
case STATE0:
func_1();
func_other();
func_yes_anothe r();
break;

/* etc. */
}

....can be made much more readable and maintainable by changing the
called functions to return a new state to the caller...

switch (state)
{
case STATE0:
func_1();
func_other();
state = func_yet_anothe r();
break;
/* etc. */
}

If the day ever comes that I need to understand and modify this
function, written by somebody else, or even by me years later, I can
see at a glance which functions might actually decide on a state
change and which cannot.

As for the trivial exception, something to allow for debugging like
this:

void change_state(st ate_type new_state)
{
#if DEBUGGING
printf("state change from %d to %d\n", state, new_state);
#endif
state = new_state;
}

Now:

switch (state)
{
case STATE0:
func_1();
func_other();
change_state(fu nc_yet_another( ));
break;
/* etc. */
}

....is considered to meet the requirements.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
Nov 13 '05 #7
In comp.os.linux.d evelopment.apps Capstar <sp***@eg.homei p.net> wrote:
Looking through the code I found out that lots of functions, which were
intended to alter the data structure would contain only one or two
lines. So I wrote macros to speed things up.
Bad move. Why don't you use inline function if you really need to?
Looking through the assembly I discovered that the compiler would
leave some function calls completely out because of the optimisation
of the code including the macro.

So now for my real question, what is your opinion on using global
variables/structures and macro's to access them in this sort of
environment?


Generally spoken: global variables are bad, macros are not better.
There are certainly occasions, where both are justified, but these are
rare. It's certainly not the common case.

Andre'
Nov 13 '05 #8
André Pönitz wrote:

In comp.os.linux.d evelopment.apps Capstar <sp***@eg.homei p.net> wrote:
Looking through the code I found out that lots of functions, which were
intended to alter the data structure would contain only one or two
lines. So I wrote macros to speed things up.


Bad move. Why don't you use inline function if you really need to?
Looking through the assembly I discovered that the compiler would
leave some function calls completely out because of the optimisation
of the code including the macro.

So now for my real question, what is your opinion on using global
variables/structures and macro's to access them in this sort of
environment?


Generally spoken: global variables are bad, macros are not better.
There are certainly occasions, where both are justified, but these are
rare. It's certainly not the common case.


I think that if a macro doesn't use any variables,
except those which are passed in as arguments,
then using the macro shouldn't become complicated.

#define SWAP(A, B, T) ((void)((T) = *(A), *(A) = *(B), *(B) = (T)))

--
pete
Nov 13 '05 #9
This is an old article from usenet which I had decided to store on my HD for
some reason. It makes for pretty fun reading and it perfectly illustrates
your point. It was originally posted by Wim Libaers
(http://groups.google.com/groups?q=g:...0020321-0051%4
0plethora.net , this version is slightly altered at the beginning)

Here are some questions on ANSI C...

Great! We all love interesting questions, especially such thought-
provoking ones!


1.
Describe the process you would use to replace a do/while loop with an
equivalent while loop.

It involves a clever technique called "editing source code". Don't tell
anyone I told you this, it's a secret!

Suppose you have been told that you must remove a while loop and
replace it with a do/while.

What additional control structure would you need to use and how would
you use it to ensure that the resulting program behaves exactly as the
original?

Why would you change the program if you wanted it to behave in exactly the
same way? Wouldn't it be better if it behaved differently? Anyway,

goto bottom;
do
{
/* any statements from inside the while loop go here */
bottom:
} while (condition);

Note that this uses a goto, which is very bad, EVIL STUFF! There is a much
better way, extremely clean, top quality code. It's a classic, too,
although it was originally used for different reasons. Credit goes to Tom
Duff.

switch (1)
{
do
{
/* loop contents here */
case 1: ;
} while (a);
}

It should be obvious immediately why this is superior compered to the
previous case. Superior to the while loop even, because we are using more
structured programming statements, so the code is increasingly getting
more and more exceedingly structured.

A criticism of the break statement and the continue statement is that
each is unstructured.

Exactly! Even worse for goto! That's why I used the structured switch
statement above.


Actually break statements and continue statements can always be
replaced by structured statements, although doing so can be awkward.
Describe in general how you would remove any break statement from a
loop in a program and replace that statement with some structure
equivalent.
Easy one. Put extra conditions in the loop condition statement for every
break to be replaced, and add if statements with the opposite condition of
the one that would have triggered the break, and enclosing everything that
follows. To prevent side-effects (why would you want to do that?) use a
flag instead of repeating the condition. Don't alter the flag anywhere
else. Do note that in some cases, a bit of extra structure is required.
For example, in replacing this one:

while (a--)
{
if (1)
{
if (c++) break;
b--;
}
b--;
}

A clean solution would be (d being the flag):

while (d && a--)
{
if (1)
{
if (d && c++) d--;
if (d)
{
b--;
}
}
if (d)
{
b--;
}
}

Clearly, the removal of the break has simplified this code enormously, and
made maintenance much easier. And added twice as much structure as in a
simple case. So, you see that a deeply nested break provides more
opportunities for beautifying code with structure until it's so structured
it almost makes your eyes hurt. Nothing is more structured than if
statements structuredly nested, so the more nested (or stacked) if
statements the more structured your code becomes.
By the way, the (d && a--) can be made nicer by using (d ? a-- : d)
instead. Same for the other one!

Describe in general how you would remove any continue statement from a
loop in a program and replace that statement with some structured
equivalent.

As above, but omitting the flag check in the condition statement of the
while loop.

Describe in general how you would remove break statements from a
switch structure and replace them with structured equivalents.

So, for example, you have:
switch (s)
{
case 1: a++; break;
case 2: b++; break;
case 3: c++; break;
default: s++;
}

Again, if is your friend!

switch (s)
{
case 1: a++;
if(0)
{
case 2: b++;
if(0)
{
case 3: c++;
if(0)
{
default: s++;
}
}
}
}
Isn't that beautiful? Not perfect though, the compiler gave me some
warnings about conditions always being false. To clarify the code and
remove the warnings, the zero in the if can be replaced by an int you give
the value zero before the loop.
A much more interesting solution is this:
switch (s)
{
case 1: if (s==1) a++;
case 2: if (s==2) b++;
case 3: if (s==3) c++;
default: if (s-1?s-2?s-3?1:0:0:0) s++;
}

Don't you just love that?
--
Martijn
http://www.sereneconcepts.nl
Nov 13 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
3333
by: lkrubner | last post by:
If I set a variable at the top of my code like this: $name = "Lawrence"; It is now a global variable. If, later on, in a function, I want to do this: function uppercaseName() { global $name;
13
1644
by: David Rysdam | last post by:
Getting no answer yesterday, I've done some investigation and I obviously don't understand how python namespaces work. Here's a test program: #!/usr/bin/python b = 2 def sumWithGlobal(a): return a + b
6
2026
by: flamesrock | last post by:
ok, so to my knowledge, object oriented means splitting something into the simplest number of parts and going from there. But the question is- when is it enough? For example I have the following code: #def put_file(file_id, delete=False): # """ Function to put the file on the FTP Server # """ # print " FTP for this file started"
16
2286
by: WaterBug | last post by:
When clicking on the following link from an email i.e - http://myserver/myapplication/myprogram.asp?urlvar1=some%20stuff&urlvar2=more%20stuff I get a server 500 error. With that same browser window open, if I click the link from the email again I get the desired page. The results are the same if the url is copied and pasted into the brower but the 'go' must be pressed twice. It appears that the server is trying to resolve the url vars...
10
2641
by: Kleenex | last post by:
Reason: I am working on an embedded project which has very limited memory (under 512 bytes, 60 or so of which is stack space), which translates into limited stack space. In order to save on stack space, I tried to only use parameters and stack space for things which are truely temporary. Instead of passing a pointer to a data structure which should always be populated with data, I have the data structure declared as a global variable and...
9
8656
by: CDMAPoster | last post by:
About a year ago there was a thread about the use of global variables in A97: http://groups.google.com/group/comp.databases.ms-access/browse_frm/thread/fedc837a5aeb6157 Best Practices by Kang Su Gatlin, casual mention was made about using static variables as an alternative to using global variables. This caused me to think of the following: '-----Begin module code
1
1291
by: Konstantinos Pachopoulos | last post by:
Hi, i had posted earlier for not being able to declare global vars. No i followed the suggestions and created a class, but still the vars do not seem to have a global scope. I have tried pretty much everything. Any advice appreciated... Here: ======================================================== #!/usr/bin/env jython #imports
4
1756
by: pcaisse | last post by:
I'm having issues sharing global variables with Explorer. This problem probably has a simple answer (as with most newbie questions). The script.pl file: #!/usr/bin/perl -w use strict; use diagnostics; use sigtrap;
0
1441
by: Gary Herron | last post by:
Jacob Davis wrote: Yuck, YUCK, YUCK! You are breaking *so* many good-programming-practices, I hardly know where to start. First off: A python global is not what you think. There are *no* program wide globals. There are only module wide globals. Also, the "global isglobal" is absolutely meaningless as anything declared there is a (module level) global by definition.
0
9568
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9398
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10156
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10007
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9832
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8831
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7375
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6649
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3924
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.