473,473 Members | 1,425 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Why are global variables considered bad?

83 New Member
I was thinking of adding some global vars to my program when I stoped to think why they are considered harmful.
I read on a google result that they can get mixed up with local variables, but that can be simply fixed with a prepended global_ or something on the lines of that.

I remember reading somewhere about them causing security or memory leaks or something like that.

Is that why? or is there more to it?
Sep 7 '09 #1
4 5287
JosAH
11,448 Recognized Expert MVP
Global variables are not so harmful by themselves as well as they are very 'volatile' in a sense that everything can change those variables at will. If a global variable doesn't contain the value you'd expected you have to scrutinize all your code to find the quilty part.

Global variables also don't go well with more than one thread of execution.

kind regards,

Jos
Sep 7 '09 #2
weaknessforcats
9,208 Recognized Expert Moderator Expert
Read this first: http://bytes.com/topic/c/insights/73...obal-variables

Usually a global variable is a replacement for thought.
Sep 7 '09 #3
donbock
2,426 Recognized Expert Top Contributor
@JosAH
Actually, this criticism applies to any variable with static duration. Such variables can be either global or local.

In C, global variables always have static duration; while local variables only have static duration if they are defined with the static keyword. C++ may have additional ways to specify a variable with static duration.
Sep 8 '09 #4
weaknessforcats
9,208 Recognized Expert Moderator Expert
In C, global variables always have static duration;
You may be confusing scope with linkage.

That is, in C global variables have global scope. That is, they can be accessed by any function in the implementation file where the global variable is defined.

To do that, they must exist for the life of the program.

Now if the global is defined as static, it means the linkage is internal. Variables with internal linkage are accessible only in the current implementation file. By default, global variables have external linkage which means they can be accessed from other implementation files if those files use the extern storage class specifier.

For local variables in functions, coding them static means that they are acessible only in the current implementation file (where the function is defined) plus the static allows the variable to exist after the function completes. So, it has internal linkage and function scope. Static global variables also have internal linkage but file scope.
Sep 8 '09 #5

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

Similar topics

13
by: Larry L | last post by:
I have a Module that declares several arrays as public, some string, some integers. I then have 2 forms. Form A is the main form, that loads on start-up, and has a command button to open Form B. On...
35
by: whisper | last post by:
My question is whether it is better to use a global variable to hold a dynamically malloced multidim array or pass around pointers to it. The details are below (forgive the long winded explanation)...
25
by: Daniel Bass | last post by:
how do i declare a global variable in c#.net? it's like it want's everything in classes... there are times when globals are good, like having constants in a program which apply to several...
8
by: newbie | last post by:
Hello, I have questions about global variables in OOP (in general) and Python (in specific). I understand (I think) that global variables are generally not a good idea. However, if there are...
8
by: yossi.kreinin | last post by:
Hi! When are multiple definitions of global variables with the same name considered legal in C, and how is it different from C++? It appears that in terms of assembly language, some C...
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...
9
by: Ed Jensen | last post by:
I'm having a vexing problem with global variables in Python. Please consider the following Python code: #! /usr/bin/env python def tiny(): bar = for tmp in foo: bar.append(tmp) foo = bar
18
by: StephQ | last post by:
I'm facing the following problem: I'm using the Gnu Scientific Library (it is a C math library), and in particular random number generation algorithms. Example code is: .... gsl_rng * r; .......
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...
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
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,...
0
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...
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...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.