473,231 Members | 1,706 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,231 software developers and data experts.

global variables

Hello!

I know it's bad design to use global variables. I just want to ask a
question about them.

Is global variables and global static variables the same. These are define
outside any function at the top of the a file where you have them.

//Tony
Jul 23 '05 #1
9 2344
Topic titled "Difference between a global int and a global static int"
seems to be up your alley:
http://groups-beta.google.com/group/...589a26025956fe

Topic titled "global static variable help" has some useful info:
http://groups-beta.google.com/group/...f744d2fa6b2ded

Topic titled "Static needed on global variables" is anothe good one I
think:
http://groups-beta.google.com/group/...eb276814d2da0b

You can find lots of info by hitting the "Groups" link at google.com
and searching there, which is how I found the above posts and many more.

Jul 23 '05 #2
On 2005-05-20, Tony Johansson <jo*****************@telia.com> wrote:
Hello!

I know it's bad design to use global variables. I just want to ask a
question about them.

Is global variables and global static variables the same. These are define
outside any function at the top of the a file where you have them.


If it's declared as static, it has "internal linkage". This means it is not
visible outside the translation unit in which it is defined. In C++, there is
not so much need for file globals, but in C they can be useful. For example,
if your C program consists of several modules, each which need to store some
state information, then each module can be implemented as a .c file, and then
the state information can be stored in internal-linkage global variables.
Basically, it's one of many ways to do encapsulation in C.

In C++, I'd be less inclined to create global variables of any kind. I'd be more
likely to implement a module system by usually putting state information in
classes and using inheritance to allow different modules to have a common
interface but differing state information.

Cheers,
--
Donovan Rebbechi
http://pegasus.rutgers.edu/~elflord/
Jul 23 '05 #3
On 2005-05-20, Donovan Rebbechi <ab***@aol.com> wrote:
On 2005-05-20, Tony Johansson <jo*****************@telia.com> wrote:
Hello!

I know it's bad design to use global variables. I just want to ask a
question about them.

Is global variables and global static variables the same. These are define
outside any function at the top of the a file where you have them.


[snip]

Just an add-on -- one can also have internal linkage for functions, which is
very useful. In C++, you usually do this via unnamed namespaces.

Cheers,
--
Donovan Rebbechi
http://pegasus.rutgers.edu/~elflord/
Jul 23 '05 #4
Tony Johansson wrote:
I know it's bad design to use global variables.
It's bad luck to make primitive things globally mutatible.
I just want to ask a
question about them.

Is global variables and global static variables the same. These are define
outside any function at the top of the a file where you have them.


'static' data variables at file scope are an excellent encapsulation
technique. They are not the same because if two translation units both see

static int foo;

then each gets its own copy of foo. Changes to one foo won't affect the
other file, and changes are the biggest problem with global variables.

--
Phlip
http://www.c2.com/cgi/wiki?ZeekLand
Jul 23 '05 #5
Donovan Rebbechi wrote:
On 2005-05-20, Donovan Rebbechi <ab***@aol.com> wrote:
On 2005-05-20, Tony Johansson <jo*****************@telia.com> wrote:
Hello!

I know it's bad design to use global variables. I just want to ask a
question about them.

Is global variables and global static variables the same. These are define
outside any function at the top of the a file where you have them.

[snip]

Just an add-on -- one can also have internal linkage for functions, which is
very useful. In C++, you usually do this via unnamed namespaces.


Wrong. In C++ any function in an unnamed namespace still has external
linkage by default. You give internal linkage to an object or a function
if you declare it 'static' or 'const' without 'extern' (for objects).

V
Jul 23 '05 #6
* Tony Johansson:

I know it's bad design to use global variables. I just want to ask a
question about them.

Is global variables and global static variables the same. These are define
outside any function at the top of the a file where you have them.


This is not primarily a language question but a terminology question.

With respect to a some piece of C++ source code S a variable is "global"
if it's available in all of S plus possibly in code outside S, and it's
"local" if it's only available within S, possibly not within all of S.

I.e.,

"local" => inside only
"global" => all of inside + possibly also outside

Used without qualification the meaning is somewhat language dependent; in
C++ unqualified "local" usually refers to S = a function, and unqualified
"global" usually refers to S = a translation unit, then meaning a variable
that's not only available throughout S but also outside S, in all units.

With that default in place, a "global variable" (unqualified) means a
variable available throughout the whole statically linked program, S =
compilation unit, whereas a "global static variable" (qualified) means
a variable available throughout a single compilation unit, S = the code
from the variable declaration to the end of the compilation unit text.

In standard C++ available throughout the complete statically linked program
is the most global a variable can be.

In in-practice C++ it's possible for a variable to be even more global,
exported to dynamically linked libraries.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 23 '05 #7
actually when you say,
int variable;
outside of all function , it means
extern int variable; automatically which means, in another translation
unit, I can declare
extern int variable , and will be able to access the value,
but
static int variable; will not allow you to do that.
But how that is done ?, every translation unit has something called
"exported" symbols, in that
'variable' will be included if you write ( int variable;) at the global
level.

Kannan Somasekar

Jul 23 '05 #8
On 2005-05-20, Victor Bazarov <v.********@comAcast.net> wrote:
Wrong. In C++ any function in an unnamed namespace still has external
linkage by default. You give internal linkage to an object or a function
if you declare it 'static' or 'const' without 'extern' (for objects).


Doh! So it does. I suppose there's a reason for this. When would one want/need
a function in an unnamed namespace to have external linkage ?

Cheers,
--
Donovan Rebbechi
http://pegasus.rutgers.edu/~elflord/
Jul 23 '05 #9
* Donovan Rebbechi:
On 2005-05-20, Victor Bazarov <v.********@comAcast.net> wrote:
Wrong. In C++ any function in an unnamed namespace still has external
linkage by default. You give internal linkage to an object or a function
if you declare it 'static' or 'const' without 'extern' (for objects).


Doh! So it does. I suppose there's a reason for this. When would one want/need
a function in an unnamed namespace to have external linkage ?


One usage is as a template argument.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 23 '05 #10

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

Similar topics

10
by: Matt | last post by:
Greetings, What are people's thoughts on global variables in C++? Why are we taught not to use them in programming? Is it true that if you are running two copies of the C program one copy can...
4
by: Andrew V. Romero | last post by:
I have been working on a function which makes it easier for me to pull variables from the URL. So far I have: <script language="JavaScript"> var variablesInUrl; var vArray = new Array(); ...
12
by: David WOO | last post by:
Hi, I am a newbie on C++, I need to define some global variables which should be accessible to most classes. In the mean time, I don't won't the global variables be modified freely at most of...
2
by: Bryan Parkoff | last post by:
.I would like to know which is the best optimization to use global variable or global struct. I always tell C/C++ Compiler to turn on optimization. .I use underscore between first name and...
17
by: MLH | last post by:
A97 Topic: If there is a way to preserve the values assigned to global variables when an untrapped runtime error occurs? I don't think there is, but I thought I'd ask. During development, I'm...
33
by: MLH | last post by:
I've read some posts indicating that having tons of GV's in an Access app is a bad idea. Personally, I love GVs and I use them (possibly abuse them) all the time for everything imaginable - have...
9
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...
5
by: Sandman | last post by:
I dont think I understand them. I've read the section on scope in the manual inside out. I'm running PHP 5.2.0 Here is the code I'm working on: //include_me.php <?php $MYVAR = array(); global...
1
weaknessforcats
by: weaknessforcats | last post by:
C++: The Case Against Global Variables Summary This article explores the negative ramifications of using global variables. The use of global variables is such a problem that C++ architects have...
112
by: istillshine | last post by:
When I control if I print messages, I usually use a global variable "int silent". When I set "-silent" flag in my command line parameters, I set silent = 1 in my main.c. I have many functions...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

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.