Information about Global Variable

In computer programming, a global variable is a variable that is accessible in every scope. Interaction mechanism with global variables are called global environment (see also global state) mechanisms. The global environment paradigm is contrasted with the local environment paradigm, where all variables are local with no shared memory (and therefore all interactions can be reconducted to message passing).

They are usually considered bad practice precisely because of their nonlocality: a global variable can potentially be modified from anywhere, and any part of the program may depend on it. A global variable therefore has an unlimited potential for creating mutual dependencies, and adding mutual dependencies increases complexity. See Action at a distance. However, in a few cases, global variables can be suitable for use. For example, they can be used to avoid having to pass frequently-used variables continuously throughout several functions.

Global variables are used extensively to pass information between sections of code that don't share a caller/callee relation like concurrent threads and signal handlers. Languages where each file defines an implicit namespace eliminate most of the problems seen with languages with a global namespace though some problems may persist without proper encapsulation. Without proper locking (such as with a mutex), code using global variables will not be thread-safe.

An example of a global variable in C++:
>
#include 
int global = 3; // This is the global variable.
void ChangeGlobal()
{
   global = 5; // Reference to global variable in a function.
}
int main()
{
   std::cout << global << endl; // Reference to global variable in another function.
   ChangeGlobal();
   std::cout << global << endl;
   return 0;
}
As the variable was a global one, there were no need to pass it as a parameter to use it in a function besides main. The global variable belongs to every function in the program. The output will be: 3 5

The use of global variables makes software harder to read and understand. Since any code anywhere in the program can change the value of the variable at any time, understanding the use of the variable may entail understanding a large portion of the program. They make separating code into reusable libraries more difficult because many systems (such as DLLs) don't directly support viewing global variables in other modules. They can lead to problems of naming because a global variable makes a name dangerous to use for any other local or object scope variable. A local variable of the same name can shield the global variable from access, again leading to harder to understand code. The setting of a global variable can create side effects that are hard to understand and predict. The use of globals make it more difficult to isolate units of code for purposes of unit testing, thus they can directly contribute to lowering the quality of the code.

See also

References

  • William Wulf and Mary Shaw, “Global Variable Considered Harmful”, ACM SIGPLAN Notices, volume 8, issue 2, 1973 February, pp. 28-34.
Computer programming (often shortened to programming or coding) is the process of writing, testing, and maintaining the source code of computer programs. The source code is written in a programming language.
..... Click the link for more information.
In computer programming, scope is an enclosing context where values and expressions are associated. Various programming languages have various types of scopes. The type of scope determines what kind of entities it can contain and how it affects them -- or semantics.
..... Click the link for more information.
shared memory is a memory that may be simultaneously accessed by multiple programs with an intent to provide communication among them. Depending on context, programs may run on the same physical processor or on separate ones.
..... Click the link for more information.
In computer science, message passing is a form of communication used in concurrent computing, parallel computing, object-oriented programming, and interprocess communication. Communication is made by the sending of messages to recipients.
..... Click the link for more information.


Action at a distance is an anti-pattern (a recognized common error) in which behavior in one part of a program varies wildly based on difficult or impossible to identify operations in another part of the program.
..... Click the link for more information.
Mutual exclusion (often abbreviated to mutex) algorithms are used in concurrent programming to avoid the simultaneous use of a common resource, such as a global variable, by pieces of computer code called critical sections.
..... Click the link for more information.
Thread safety is a computer programming concept applicable in the context of multi-threaded programs. A piece of code is thread-safe if it functions correctly during simultaneous execution by multiple threads.
..... Click the link for more information.
C++
Paradigm: Multi-paradigm
Appeared in: 1983
Designed by: Bjarne Stroustrup
Typing discipline: Static, unsafe, nominative
Major implementations: G++, Microsoft Visual C++, Borland C++ Builder
Dialects: ISO/IEC C++ 1998, ISO/IEC C++ 2003
..... Click the link for more information.
DLL is an abbreviation which can commonly mean:
  • Data link layer, a layer in the OSI network architecture model
  • Delay-locked loop, a device to reduce clock skew in digital circuits
  • Doubly-linked list, a data structure in computer programming

..... Click the link for more information.
variable (IPA pronunciation: [ˈvæɹiəbl]) (sometimes called a pronumeral) is a symbolic representation denoting a quantity or expression.
..... Click the link for more information.
static variables typically have a broader scope than other variables. Their values may be set at run-time or may change subtly during program execution.

For constants


..... Click the link for more information.

The Basic Definition

An external variable (keyword extern for C and C++ programming languages) is used to connect a single global variable across multiple project files.
..... Click the link for more information.
In software engineering, the singleton pattern is a design pattern that is used to restrict instantiation of a class to one object. This is useful when exactly one object is needed to coordinate actions across the system.
..... Click the link for more information.
William Allan Wulf (born December 8, 1939) is a computer scientist notable for his work in programming languages and compilers.

Born in Chicago, Illinois, he attended the University of Illinois, receiving a BS in Engineering Physics and an MS in Electrical Engineering, then
..... Click the link for more information.


This article is copied from an article on Wikipedia.org - the free encyclopedia created and edited by online user community. The text was not checked or edited by anyone on our staff. Although the vast majority of the wikipedia encyclopedia articles provide accurate and timely information please do not assume the accuracy of any particular article. This article is distributed under the terms of GNU Free Documentation License.
Herod_Archelaus


page counter