Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > global variables are bad?

Reply
Thread Tools

global variables are bad?

 
 
fabio
Guest
Posts: n/a
 
      03-11-2006
Why? i' ve heard about this, the usage of global vars instead of
locals is discouraged, but why?
thx
 
Reply With Quote
 
 
 
 
Michael Mair
Guest
Posts: n/a
 
      03-11-2006
fabio schrieb:
> Why? i' ve heard about this, the usage of global vars instead of
> locals is discouraged, but why?


There is no such thing as a global variable in C.
What you probably mean is a variable with static storage duration
and external linkage. Why make such a fuss about denomination? The
storage duration and the linkage determine different sides of
"global".

Linkage: External linkage (implies file scope and) means that the
identifier (the "variable name") must be the only one with exactly
this name and linkage throughout the whole programme. As soon as
there is a declaration of the variable in scope, it is "visible"
and accessible in the respective scope. This can be another
translation unit; what does this mean? You are using the variable
for an internal status record and now someone, maybe even you
yourself, thoughtlessly or seemingly cleverly overwrites this
status. This can easily upset the internal workings of your
programme after some changes.
You now can reduce the visibility and accessibility of the variable
by giving it internal linkage (the declaration remains on file level
but is preceded by "static") which means it is only accessible within
one translation unit -- as long as you do not give its address to
some part of the programme outside --, or by giving it block scope and
no linkage (the declaration is within a function and preceded by
"static") which means it is only accessible from its declaration to
the closing } -- as long as you do not give its address to
some part of the programme outside.

Storage duration: Static storage duration means that your variable
exists throughout the whole life-time of your programme. It is
initialized only once and holds its value between two writing
accesses. How can this be bad?
1) Whatever part of your programme directly knows the variable or
at least knows its address, can modify it.
2) If a function uses this variable to keep track of some sort of
internal status and expects only certain status transitions, then
calling the function from two different points in your programme
can upset the function's logic and lead to invalid results. This
can be especially an issue if the function is part of a library
and this library is accessed by more than one programme or if you
have a multithreaded programme -- this situation is not covered by
standard C, so I will not go into detail. Read up on "reentrancy"
to understand the issues involved.

In addition, if you have more than one place in your programme
where a variable is used or modified, even if you all planned it
out carefully and have none of the situations described above, you
can more easily make a mistake when designing the algorithm using
this variable than when you just pass it or its address around in
a strictly controlled manner.

Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
 
Reply With Quote
 
 
 
 
Richard G. Riley
Guest
Posts: n/a
 
      03-11-2006
"Michael"posted the following on 2006-03-11:

> fabio schrieb:
>> Why? i' ve heard about this, the usage of global vars instead of
>> locals is discouraged, but why?

>
> There is no such thing as a global variable in C.


http://www.google.com/search?q=global+variables+in+c

> What you probably mean is a variable with static storage duration
> and external linkage. Why make such a fuss about denomination? The
> storage duration and the linkage determine different sides of
> "global".


Assuming he means the commmonly understood meaning of global variable
here is another answer:

globals are nasty (generally) when used in certain functions which
would be better off having local variables since there is no
programmatic need to share that data with any code outside of that
subset or class of functions. Things you wish to share are passed back
by pointers or as values. Occasionally a global structure can be used to.

The bottom line is that making things available to code which
doesnt need them is bad : it is not structured and it is not
modularised.

You do not want someone linking to your "global" when YOU
require it to maintain its integrity between invocations of your code
and not be accessible to the whims of every tom, dick and harry who
have added modules to your system.

There are thousands of articles on this on the web.


>
> Linkage: External linkage (implies file scope and) means that the
> identifier (the "variable name") must be the only one with exactly
> this name and linkage throughout the whole programme. As soon as
> there is a declaration of the variable in scope, it is "visible"
> and accessible in the respective scope. This can be another
> translation unit; what does this mean? You are using the variable
> for an internal status record and now someone, maybe even you
> yourself, thoughtlessly or seemingly cleverly overwrites this
> status. This can easily upset the internal workings of your
> programme after some changes.
> You now can reduce the visibility and accessibility of the variable
> by giving it internal linkage (the declaration remains on file level
> but is preceded by "static") which means it is only accessible within
> one translation unit -- as long as you do not give its address to
> some part of the programme outside --, or by giving it block scope and
> no linkage (the declaration is within a function and preceded by
> "static") which means it is only accessible from its declaration to
> the closing } -- as long as you do not give its address to
> some part of the programme outside.
>
> Storage duration: Static storage duration means that your variable
> exists throughout the whole life-time of your programme. It is
> initialized only once and holds its value between two writing
> accesses. How can this be bad?
> 1) Whatever part of your programme directly knows the variable or
> at least knows its address, can modify it.
> 2) If a function uses this variable to keep track of some sort of
> internal status and expects only certain status transitions, then
> calling the function from two different points in your programme
> can upset the function's logic and lead to invalid results. This
> can be especially an issue if the function is part of a library
> and this library is accessed by more than one programme or if you
> have a multithreaded programme -- this situation is not covered by
> standard C, so I will not go into detail. Read up on "reentrancy"
> to understand the issues involved.
>
> In addition, if you have more than one place in your programme
> where a variable is used or modified, even if you all planned it
> out carefully and have none of the situations described above, you
> can more easily make a mistake when designing the algorithm using
> this variable than when you just pass it or its address around in
> a strictly controlled manner.
>
> Cheers
> Michael



--
"A desk is a dangerous place from which to view the world" - LeCarre.
 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      03-11-2006
Richard G. Riley said:

> "Michael"posted the following on 2006-03-11:
>>
>> There is no such thing as a global variable in C.

>
> http://www.google.com/search?q=global+variables+in+c


http://www.google.com/search?q=tooth+fairy (nearly four million hits).

Just cos it's on Google, doesn't mean it exists.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
 
Reply With Quote
 
Richard G. Riley
Guest
Posts: n/a
 
      03-11-2006
"Richard"posted the following on 2006-03-11:

> Richard G. Riley said:
>
>> "Michael"posted the following on 2006-03-11:
>>>
>>> There is no such thing as a global variable in C.

>>
>> http://www.google.com/search?q=global+variables+in+c

>
> http://www.google.com/search?q=tooth+fairy (nearly four million hits).
>
> Just cos it's on Google, doesn't mean it exists.
>


You dont say? But when we find x thousand courses which are quite
happy to see "global variable" talked about in C then does it seem
quite right to start pretending it doesnt apply anymore in ng? No. Did
you read the initial reply? Did you think that was constructive for a newbie?

Yoou dont know or agree with the common meaning of "global variable" in C?

 
Reply With Quote
 
osmium
Guest
Posts: n/a
 
      03-11-2006
"Richard Heathfield" wrote:

> http://www.google.com/search?q=tooth+fairy (nearly four million hits).
>
> Just cos it's on Google, doesn't mean it exists.


And my personal favorite, "abraham lincoln" "quantum physics" 68K hits.

http://www.google.com/search?hl=en&q...=Google+Search


 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      03-11-2006
Richard G. Riley said:

> "Richard"posted the following on 2006-03-11:
>
>> Richard G. Riley said:
>>
>>> "Michael"posted the following on 2006-03-11:
>>>>
>>>> There is no such thing as a global variable in C.
>>>
>>> http://www.google.com/search?q=global+variables+in+c

>>
>> http://www.google.com/search?q=tooth+fairy (nearly four million hits).
>>
>> Just cos it's on Google, doesn't mean it exists.
>>

>
> You dont say? But when we find x thousand courses which are quite
> happy to see "global variable" talked about in C then does it seem
> quite right to start pretending it doesnt apply anymore in ng?


The word "variable" isn't all that useful, and the term "global variable" is
less useful still.

> No. Did
> you read the initial reply?


Yes.

> Did you think that was constructive for a newbie?


Yes. No point lyin' to 'em.

> Yoou dont know or agree with the common meaning of "global variable" in
> C?


The term has no universally recognised meaning in C.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
 
Reply With Quote
 
Malcolm
Guest
Posts: n/a
 
      03-11-2006
"Richard Heathfield" <(E-Mail Removed)> wrote
>> Yoou dont know or agree with the common meaning of "global variable" in
>> C?

>
> The term has no universally recognised meaning in C.
>

Neither does "linked list". Or "pure function". Doesn't mean that such a
think doesn't exist.
--
Buy my book 12 Common Atheist Arguments (refuted)
$1.25 download or $6.90 paper, available www.lulu.com


 
Reply With Quote
 
Richard G. Riley
Guest
Posts: n/a
 
      03-11-2006
On 2006-03-11, Richard Heathfield <(E-Mail Removed)> wrote:
> Richard G. Riley said:
>
>> "Richard"posted the following on 2006-03-11:
>>
>>> Richard G. Riley said:
>>>
>>>> "Michael"posted the following on 2006-03-11:
>>>>>
>>>>> There is no such thing as a global variable in C.
>>>>
>>>> http://www.google.com/search?q=global+variables+in+c
>>>
>>> http://www.google.com/search?q=tooth+fairy (nearly four million hits).
>>>
>>> Just cos it's on Google, doesn't mean it exists.
>>>

>>
>> You dont say? But when we find x thousand courses which are quite
>> happy to see "global variable" talked about in C then does it seem
>> quite right to start pretending it doesnt apply anymore in ng?

>
> The word "variable" isn't all that useful, and the term "global variable" is
> less useful still.
>
>> No. Did
>> you read the initial reply?

>
> Yes.


I did not think that reply in anyway
clarified anything.

>
>> Did you think that was constructive for a newbie?

>
> Yes. No point lyin' to 'em.
>


One has to be practical. Not too clever for ones own good sometimes.

>> Yoou dont know or agree with the common meaning of "global variable" in
>> C?

>
> The term has no universally recognised meaning in C.
>


Neither do lots of things : but the art is in recognising the meaning
from a newbie. Any half decent programmer worth his salt knows and
recognises what is meant by a global variable and in that context C
certainly does support them and the reasons for not using them are as
prevalent as in other languages : which is, fater all, what the OP was
asking. I dont know, maybe I'm getting too old for this : there was a
time when people wanted to help : not just get one upsmanship points
on who can read the standards the best.

 
Reply With Quote
 
Michael Mair
Guest
Posts: n/a
 
      03-11-2006
Richard G. Riley schrieb:
> On 2006-03-11, Richard Heathfield <(E-Mail Removed)> wrote:
>>Richard G. Riley said:
>>> "Richard"posted the following on 2006-03-11:
>>>>Richard G. Riley said:
>>>>
>>>>> "Michael"posted the following on 2006-03-11:
>>>>>
>>>>>>There is no such thing as a global variable in C.
>>>>>
>>>>>http://www.google.com/search?q=global+variables+in+c
>>>>
>>>>http://www.google.com/search?q=tooth+fairy (nearly four million hits).
>>>>
>>>>Just cos it's on Google, doesn't mean it exists.
>>>
>>>You dont say? But when we find x thousand courses which are quite
>>>happy to see "global variable" talked about in C then does it seem
>>>quite right to start pretending it doesnt apply anymore in ng?

>>
>>The word "variable" isn't all that useful, and the term "global variable" is
>>less useful still.
>>
>>>No. Did
>>>you read the initial reply?

>>
>>Yes.

>
> I did not think that reply in anyway
> clarified anything.


You could have posted a direct reply.
You just could have expanded on mine and said that you consider
it too technical to be of any use to a newbie -- and be done.

For me, it was important to clarify that there are three
conceptual sides to it:
- Linkage
- Storage duration
- Ease or difficulty of seeing whether the implemented design
is the intended one, let alone correct

The former two are joined to the latter but may give toothaches
on their own. If I failed to carry this point over, then shame
on me. If you understood it but did not clarify it to suffice
your standards, shame on you.
In addition, it _is_ important to know what you are talking
about. The concepts mentioned should help the OP find his way
through it and also find out which side leads to which problem.
It may very well be that he decides to use a "global" variable
with internal linkage because this is exactly what he needs --
after having thought about why he does not want the uncertainties
involved in an object with external linkage and potential problems
when linking.


>>>Did you think that was constructive for a newbie?

>>
>>Yes. No point lyin' to 'em.

>
> One has to be practical. Not too clever for ones own good sometimes.


This is true.
Nothing ever is simple, though, so I consider it fair to tell
people a little bit more.
A perfect reply certainly would be one of Chris Torek type (useful,
clear, concise, written nicely) but I certainly am not up to that
as I am neither a teacher nor a native speaker.


>>>Yoou dont know or agree with the common meaning of "global variable" in
>>>C?

>>
>>The term has no universally recognised meaning in C.

>
> Neither do lots of things : but the art is in recognising the meaning
> from a newbie. Any half decent programmer worth his salt knows and
> recognises what is meant by a global variable and in that context C
> certainly does support them and the reasons for not using them are as
> prevalent as in other languages : which is, fater all, what the OP was
> asking. I dont know, maybe I'm getting too old for this : there was a
> time when people wanted to help : not just get one upsmanship points
> on who can read the standards the best.


The latter was not my intention. I post here to help and to
learn. I certainly do not pride myself on my often spotty knowledge
of the standard. You will guaranteedly find a couple of times
within the last month where I made a mistake and was glad because
it was corrected.
If you are saying that my reply was insufficient: You were free
to improve it. Next time I will take what you said into account
when answering a similar question.
If you are saying that I did it intentionally to show off, then I
think you are just once more out to look for an unnecessary fight
to pick -- and you are plain wrong.


Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Initialize global variable before any other global variables jubelbrus C++ 5 07-20-2007 06:38 PM
FWSM/PIX and Dynamic PAT using global IP range vs. global interface vs. global IP Hoffa Cisco 1 10-25-2006 06:50 PM
FWSM/PIX and Dynamic PAT using global IP range vs. global interface vs. global IP Hoffa Cisco 0 10-25-2006 01:04 PM
Global variables on par with ASP's global.asa Wayne ASP .Net 2 11-11-2003 10:58 PM
Global variables - application variables vs include file mark4asp ASP General 1 09-03-2003 01:30 PM



Advertisments