473,903 Members | 3,258 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2384
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.co m> 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.co m> 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.********@com Acast.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.********@com Acast.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
17901
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 overwrite another copies global variable? I know that you could overwrite a value in a global variable from a function, but you could also do that if you pass the variable in and then out again... so how is that any different?
4
24193
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(); function loadUrlVariables() { varString = location.search;
12
5896
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 these classes. I know there is a pattern called singleton can more or less do such a trick. I am wondering is this the best way to do it (regarding the convenience and safety), as this is such a fundamental thing, I believe most of you have a say...
2
5196
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 second name for better readable. After optimization, global variables might be misaligned because each global variables must be converted to 32 bits, but I do see that C/C++ Compiler do padding between variables. Struct does the same to do padding....
17
5656
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 constantly running tests on imperfect code. On of the cumbersome jobs encountered is reassigning global vars their values after a close encounter with an untrapped runtime error. Rather than writing a procedure to simply reassign them all with a...
33
3076
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 been for years. If the machine has memory to spare and windows can use it - I'm thinking "Why not?" I was wondering what some of you have to say about that, particularly any severe "gotchas" you've had the unfortunate experience to contend with.
9
8680
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
5
11840
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 $MYVAR, $a; ?>
1
29418
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 called it polluting the global namespace. This article explores what happens when the global namespace becomes polluted and how to avoid this condition. The opinions expressed in this article are those of the author alone although many have...
112
5518
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 that may print some messages. foo(...) { if (!silent)
0
11282
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
10873
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...
1
10983
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7206
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();...
0
5894
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6088
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4726
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
2
4308
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3324
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.