Why global variables are considered harmful.

Like the infamous goto statement, the use of global variables in programs has been hotly debated over the past decades. Why? – because if used incorrectly they (like goto) can lead to bad programming practices. So what is a global variable? Well, a global variable is one whose scope is effectively the whole program. For example:

#include <stdio.h>

int aGlobal;

int aFunc(int a)
{
    printf(“%d”, aGlobal);
}

int main(void)
{
    printf(“%d”, aGlobal);

    return 0;
}

Here the variable aGlobal is a global variable – it can be accessed both in the function aFunc, and main. Normally, variables stored on the stack are temporal in nature, i.e. when the function aFunc ends, all its variables are destroyed, and the memory associated with them in the stack is reclaimed. Global variables aren’t stored in the stack, but rather in a secluded piece of memory sometimes referred to as static memory (also where static variables are stored, since they must retain their value between invocations of a function).

So why are global variables bad? Firstly they are non-local, which means they are often harder to keep track of, as they can be modified anywhere in the program – anywhere. As the number of parts in the program that use global variables increases, so too does the complexity of the interactions between program components (i.e. increased coupling). One of the purposes of modularity is to decrease coupling in order to improve program maintainability.  Another problem is namespace pollution, in other words variables may be unknowingly used because you think you are using a local variable (with the same name). Take for example the following piece of code:

#include <stdio.h>

int a = 6; //global

int main (void)
{
    int a = 12; //local
    printf("%d\n", a); 

    return 0;
}

What happens here? The value 12 is output, with the local variable a declared in main effectively overriding the global variable a.

So where can they be used?

There are situations where the use of a global variable becomes more meaningful. A good example is the creation of a user-defined structure which must be accessed throughout the program. Consider the example below, of a struct created to store a linked list. The structure can be access as a parameter to a function, as a structure within a function, or a structure within main. It would be impossible to create such as struct in main and have it’s core data structure available to functions elsewhere. For example:

#include <stdio.h>

struct node {
    int item;
    struct node *next;
};

int makeList(struct node *ptr)
{
    struct node *temp;
    // ...
}

int main(void)
{
    struct node *list;
    // ...
    return 0;
}

However in reality, such structures, as well as functions may be better situated in a header file.

[1] Wulf, W.A., Shaw, M., “Global Variables Considered Harmful”, ACM SIGPLAN Notices, 8(2), pp.80-86 (1973).

 

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.