473,594 Members | 2,675 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

global variables coding preference

I have been going through some of the tutorials on the internet
regarding coding styles in unix C programming. Most of the stuff I have
read do not prefer using global variables, But for multi-file programs
it is impossible not to have global variables.

I am trying to learn of the best way to check if I am mis-using global
variables. How do you guys know if there are to many global variables in
your programs? I am not trying to start a flame war on coding styles. I
just want to know how the more experienced programmers do it.

I usually just follow the Linux-Kernel Coding style, But in this case it
seems to be unclear to me.

thanks
,Fernan
--
Public PGP/GnuPG key (http://www.fernski.com)
pub 1024D/681A5498 2003-10-26 Fernan Bolando
Key fingerprint = BAC8 EA6A CA98 26F3 3DB4 74D1 988E 56F7 681A 5498
Nov 14 '05 #1
8 1760
In article <sl************ **************@ Anubis.local.ne t>,
Fernan Bolando <fe***********@ mailc.net> wrote:
I have been going through some of the tutorials on the internet
regarding coding styles in unix C programming. Most of the stuff I have
read do not prefer using global variables, But for multi-file programs
it is impossible not to have global variables.

I am trying to learn of the best way to check if I am mis-using global
variables. How do you guys know if there are to many global variables in
your programs? I am not trying to start a flame war on coding styles. I
just want to know how the more experienced programmers do it.

I usually just follow the Linux-Kernel Coding style, But in this case it
seems to be unclear to me.


Define a struct

struct TApplication;

then use a global variable

struct TApplication* gApplication;

That's one global variable, and it is enough :-)
Nov 14 '05 #2
Fernan Bolando wrote on 02/08/04 :
I have been going through some of the tutorials on the internet
regarding coding styles in unix C programming. Most of the stuff I have
read do not prefer using global variables, But for multi-file programs
it is impossible not to have global variables.
Why did God create functions with parameters?
I am trying to learn of the best way to check if I am mis-using global
variables.
Hard to say, but yes, they can be avoided in most cases, specially in
reusable code.
How do you guys know if there are to many global variables in
your programs?
There is no quota. You can have a perfectly unreadable and
unmaintenable code with exactly one global pointer to a huge structure.
This is not the point.
I am not trying to start a flame war on coding styles. I
just want to know how the more experienced programmers do it.


Using ADT for reusable objects. (the standard FILE object is a good
example)

Using encapsulation and restriced acces (read-only, accessors etc.) at
application level.

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html

"C is a sharp tool"

Nov 14 '05 #3

In article <sl************ **************@ Anubis.local.ne t>, Fernan Bolando <fe***********@ mailc.net> writes:
I have been going through some of the tutorials on the internet
regarding coding styles in unix C programming. Most of the stuff I have
read do not prefer using global variables, But for multi-file programs
it is impossible not to have global variables.


Not true. It's simple to replace global variables with dynamic or
static variables and accessor functions. The simplest approach is
probably to use a structure and a thunk that returns a pointer to it:

struct Globals
{
int Global1, Global2;
}

struct Globals *GetGlobals(voi d)
{
static struct Globals Globals;
return &Globals;
}
Now any function, in any TU ("file"), that needs access to Global1 or
Global2 need only:

struct Globals *Globals = GetGlobals();
Globals->Global1 = 1;

(Obviously, the definition of struct Globals and the prototype for
GetGlobals should be in a header included in every TU, for this sort
of arrangement.)

If you're of the school that considers static variables "hidden globals",
then dynamically allocate the thing at program startup instead.

There are many other reasonable alternatives to global variables.

--
Michael Wojcik mi************@ microfocus.com

What is it with this warm, quiet, nauseating bond between them?
-- Rumiko Takahashi, _Maison Ikkoku_, trans. Mari Morimoto, adapt. Gerard
Jones
Nov 14 '05 #4

"Fernan Bolando" <fe***********@ mailc.net> wrote in message

I am trying to learn of the best way to check if I am mis-using global
variables. How do you guys know if there are to many global variables in
your programs? I am not trying to start a flame war on coding styles. I
just want to know how the more experienced programmers do it.

The best rule is never have a global. Always use a static (file scope)
varaible instead, with get_ and set_ functions.
The advantage is largely psychological, but psychology is important.

Secondly, you only want the get_ / set_ routine where you can't think of any
other way of doing things. For instance, often it will make sense to do
validity checking in the set_ function and check for integrity in the get_
function. Often you will need to set many globals at the same time (for
instance time might need to be set in the same function as date).

However you do still want to keep the number of static globals down. Usually
a function should take its input and output via parameters, not via globals,
though this isn't always possible.
Nov 14 '05 #5
In article <ce********@new s2.newsguy.com> , Michael Wojcik wrote:

In article <sl************ **************@ Anubis.local.ne t>, Fernan Bolando <fe***********@ mailc.net> writes:
I have been going through some of the tutorials on the internet
regarding coding styles in unix C programming. Most of the stuff I have
read do not prefer using global variables, But for multi-file programs
it is impossible not to have global variables.


Not true. It's simple to replace global variables with dynamic or
static variables and accessor functions. The simplest approach is
probably to use a structure and a thunk that returns a pointer to it:

struct Globals
{
int Global1, Global2;
}

struct Globals *GetGlobals(voi d)
{
static struct Globals Globals;
return &Globals;
}
Now any function, in any TU ("file"), that needs access to Global1 or
Global2 need only:

struct Globals *Globals = GetGlobals();
Globals->Global1 = 1;

(Obviously, the definition of struct Globals and the prototype for
GetGlobals should be in a header included in every TU, for this sort
of arrangement.)

If you're of the school that considers static variables "hidden globals",
then dynamically allocate the thing at program startup instead.

There are many other reasonable alternatives to global variables.


First of all I would like to thank everyone who replied. It now have a
bit more idea on how I should approach a situation that seems to need a
global variable.
,Fernan

--
Public PGP/GnuPG key (http://www.fernski.com)
pub 1024D/681A5498 2003-10-26 Fernan Bolando
Key fingerprint = BAC8 EA6A CA98 26F3 3DB4 74D1 988E 56F7 681A 5498
Nov 14 '05 #6
Michael Wojcik wrote:
It's simple to replace global variables with dynamic or
static variables and accessor functions. The simplest approach is
probably to use a structure and a thunk that returns a pointer to it:

struct Globals
{
int Global1, Global2;
}

struct Globals *GetGlobals(voi d)
{
static struct Globals Globals;
return &Globals;
}


Why not go the exra step and have get/set functions for all the members?
That way, the global structure need not be accessed directly at all
(outside of these special functions, that is). In fact, the rest of your
program wouldn't even need to know about the struct.
Christian
Nov 14 '05 #7
Christian Kandeler <ch************ ****@hob.de> writes:
Why not go the exra step and have get/set functions for all the members?


This is a good choic though it is a very OOP approach.
Regards,

--
Gustavo G. Rondina
http://gustgr.freeshell.org
Nov 14 '05 #8

In article <2n************ @uni-berlin.de>, Christian Kandeler <ch************ ****@hob.de> writes:
Michael Wojcik wrote:
It's simple to replace global variables with dynamic or
static variables and accessor functions. The simplest approach is
probably to use a structure and a thunk that returns a pointer to it:

struct Globals
{
int Global1, Global2;
}

struct Globals *GetGlobals(voi d)
{
static struct Globals Globals;
return &Globals;
}
Why not go the exra step and have get/set functions for all the members?


See the phrase "simplest approach" above, which precludes taking an
extra step.
That way, the global structure need not be accessed directly at all
(outside of these special functions, that is). In fact, the rest of your
program wouldn't even need to know about the struct.


In practice, certainly (though there are other advantages to using a
structure for related variables, or for an "execution context" of
some sort, particularly if it's an incomplete (opaque) type outside
of the module where it's manipulated). I was demonstrating that the
assertion "you have to use global variables in some programs" was
wrong (modulo degenerate specifications which require global
variables explicitly, or prohibit the necessary language constructs,
of course).

--
Michael Wojcik mi************@ microfocus.com

He smiled and let his gaze fall to hers, so that her cheek began to
glow. Ecstatically she waited until his mouth slowly neared her own.
She knew only one thing: rdoeniadtrgove niardgoverdgovn rdgog.
Nov 14 '05 #9

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

Similar topics

14
3294
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;
10
17844
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?
2
1622
by: learner | last post by:
Hi, In a page, i have many links. I have some functions in a global file which is included in all linked pages. I want to have a reference to a window which is to be opened on clicking one link and it should be known only to one more link on that page and not to other links. I cant have that window reference in that global file, since iam declaring it initially as Null and modifying its value in that one particular link. Since all links...
19
4162
by: laurenq uantrell | last post by:
I'm using Access 2K. I'm hoping someone can tell me which method performs faster- (currently I'm using a mix of both methods) a.) creating custom properties and then calling functions to set and retrieve the values... (ie: application.currentproject.properties(propName)) b.) creating global variables in the form of an array and using a function to populate and retrieve them.
17
5612
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...
7
3119
by: Michael | last post by:
Hi newsgroup, as the subject indicates I am looking for an advice using global variables. I am not if this problem is more about style then C. If its wrong in thi group, sorry. So I have a couple of function that all need the same information (all located in the same file). By now it looks like /* file beginns */
18
8631
by: vib | last post by:
Hi there, By chance, I came to learn that it is bad programming practice to initialize global variables at outside of programs. Is it that bad? In order to fullfil this, I had to declare them with const, but thereafter they are no longer variables. Just couldn't see the benefits of this practice. Any comments or advices are welcome. Thanks vib
15
2540
by: =?Utf-8?B?UGF0Qg==?= | last post by:
Just starting to move to ASP.NET 2.0 and having trouble with the Global.asax code file. In 1.1 I could have a code behind file for the global.asax file. This allow for shared variables of the Global class. Note: I use these shared variables for read only values that are set at application start. It would appear the 2.0 doesn't like you to use shared variables in the global class. How do I convert my 1.1 applications to 2.0 without...
4
2365
by: icarus | last post by:
global_vars.py has the global variables set_var.py changes one of the values on the global variables (don't close it or terminate) get_var.py retrieves the recently value changed (triggered right after set_var.py above) Problem: get_var.py retrieves the old value, the built-in one but not the recently changed value in set_var.py. What am I doing wrong?
0
7941
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
7874
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
8246
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
8368
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
8000
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
3854
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
3895
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1476
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1205
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.