473,395 Members | 2,192 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,395 software developers and data experts.

Global Variables: Safest Use

8
Hi,


I want to use a variable in two files in an application that i am writing.

What would you suggest is the saftest way to do this, should i:

a). Define it in one of the files it will be used in and then use the
Expand|Select|Wrap|Line Numbers
  1. extern
keyword to make it accessible in the other file.

b). define it in a header file and then include the header file in both the files i want to use the variable in

If you can give any suggestions on the best way to accomplish the latter, that would be appreciated.

All suggestions and reasons welcomed.

Thanks in advance for your time.
Jul 28 '07 #1
7 1379
archonmagnus
113 100+
In my opinion, programming is an art, and everyone uses different brushstrokes--so to speak. But if I were doing it, I would put my global vars in the included header file. It just seems like it would be easier to remember where they were in such case for code maintenance. There are (potentially) valid reasons for both cases, but that is just my opinion.
Jul 28 '07 #2
weaknessforcats
9,208 Expert Mod 8TB
Your choice (a) using the extern, works to a point. The extern says the variable is not in this file. Soyou declare the variable in another file and let the linker find it.

I am currently writing an article where I have 10 cases where using a global variable will lead to disaster. Global variables are a carryover from C where the the focus was on functionality rather than data. An object program is data-centric so the individual data variables are not exposed.

Your choice (2) won't work. You will have a global varible created each time the header is included. These duplicates will cause the linker to generate errors about redefinition.

My suggestion:
1) write a function that returns the appropriate value.
2) Call that function as needed elsewhere in the program.

This will make the value inaccessible directly and should you ever need to re-design how you obtain the value all you need to is rewrite the function and recompile the application. This is far easier than going through code and removing a global variable and replacing it with something else.
Jul 28 '07 #3
Banfa
9,065 Expert Mod 8TB
My suggestion:
1) write a function that returns the appropriate value.
2) Call that function as needed elsewhere in the program.
If you need to set the value then write a that sets the value as well. This creates an abstract data type (a construct that in C++ has been completely replaced by classes), this is basically where you have all the data related to 1 thing as static members in 1 file and you access it to read and write values from other files by calling functions. There are a lot of benefits to this not least of which is if you want the underlying behaviour to change as long as you can maintain the same interface you only have to change the 1 file.
Jul 28 '07 #4
locus
8
I like your first sentence archonmagnus, and i was initially leaning toward your approach,but because of your first sentence, i am going to explore what weaknessforcats and Banfa are saying. Thanks man.

So guys let me make sure i am getting this correct;

1). I should create a header file defining a class with functions that set and retreive the value of my variable,

2). include this header in the two files that must have access to the value

3). call these functions to set and retreive the value

is this what you guys are saying?

Thanks a lot for your responses, they are appreciated.
Jul 28 '07 #5
locus
8
also should the functions used to set and retreive the value of the variable (the one i want to make global) be static public member functions of this class, with the variable be a private static member?
Jul 28 '07 #6
Banfa
9,065 Expert Mod 8TB
also should the functions used to set and retreive the value of the variable (the one i want to make global) be static public member functions of this class, with the variable be a private static member?
I am not sure about weaknessforcats but I was initially answering from a C point of view. If you are using a class then obviously you are using C++.

Between the 2 of us weaknessforcats is definitely stronger on C++ theory than me, however I will say this (and may be he will correct me later).

I would generally avoid using static data for function members in a class except in the case that there is a definite possibility that there will be multiple instances of the class and those instances require some shared data (i.e. all instances using the same value for a variable).

However you sound like what you need is actually a class that you are only going to instantiate once. There is a design pattern for this called the Singleton and weaknessforcats has written an excellent tutorial on this design pattern here. Basically in the singleton design pattern the class itself protects itself against being instantiated more than once and provides an interface that always returns the 1 instantiation of the class.

Once you have a class that implements the singleton design pattern then you add data and function members to it in the normal way. For your purposes these members should not be static but the data members should be private or protected (so they are only accessible with the class or the class and classes that derive from it) and the functions that access or operate on the data should be public (or else you wont be able to call them).
Jul 29 '07 #7
weaknessforcats
9,208 Expert Mod 8TB
(and may be he will correct me later).
Nope. You are right on target.
Jul 29 '07 #8

Sign in to post your reply or Sign up for a free account.

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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...
0
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...

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.