Javatpoint Logo
Javatpoint Logo

Why (non-const) global variables are evil in C++

In this article, you will learn about why global variables are evil in C++:

Outside of any program function, global variables are defined and declared. For the duration of the program, they uphold their ideals. Throughout the program's execution, they are available.

Global variables that are not const are evil because any function can modify their value. The program becomes less flexible and modular when global variables are used. It is advised against using program global variables. Use local variables in the program instead of global variables.

  1. Unintentional Side consequences: As global variables are accessible and editable from anywhere in the program, it might be challenging to keep track of changes that result in unexpected side consequences. The code may become more difficult to comprehend and update as a result.
  2. Namespace Pollution: Excessive use of global variables can result in namespace pollution, which is the accumulation of variable names in the global namespace, perhaps causing naming conflicts. Global variables are a part of the global namespace.
  3. Dependency on Initialization Order: Global variables may be affected by the initialization order. If it is not well controlled, it may result in unexpected behavior. It can grow to be a substantial source of faults, particularly in intricate programs with several translation units.
  4. Difficult to Test and Debug: Programs that use global variables may be more difficult to test and debug because any component of the code might change the state of the variables, making it challenging to identify and replicate individual problems.
  5. Encapsulation and Abstraction: Fundamental concepts of object-oriented programming include encapsulation and abstraction, or the concealing of data. Encapsulation can be broken by using global variables since any section of the program can access and change the global state, making data security and integrity management difficult.

Local variables or passing variables explicitly as function parameters are frequently suggested as a way to reduce the usage of global variables and help mitigate these problems. Reducing the dependence on global variables and improving data management are further benefits of encapsulating data within classes and employing suitable access modifiers (such as private and protected). Intended alterations can also be avoided, and immutability can be enforced when appropriate by using const global variables.

Example:

Output:

Initial value of the global variable: 5
Global variable value: 10
New local variable value: 20
Global variable value: 10

Explanation:

  1. #include <iostream>: You may use the std::cout to print output to the console by including the input/output stream library in this line.
  2. int globalVariable = 5;: This line initializes the global integer variable globalVariable to the value 5. Any function in the program can access and modify global variables, which are defined outside of all other functions.
  3. void modify_Global_Variable(): This line defines the modify_Global_Variable() method, which has no parameters and returns void. The global variable globalVariable is updated inside this method to contain the number 10.
  4. void print_Global_Variable(): This line defines the function print_Global_Variable(). It has no parameters and yields void as its return value. The global variable globalVariable's value is simply printed to the console by this function.
  5. Within the function main():
    • std::cout << "Initial value of the global variable: " << globalVariable << std::endl; prints the initial value of the global variable to the console, which is 5.alter_Global_Variable(); calls the alter_Global_Variable() method, setting the global variable's value to 10.
    • print Global Variable(); prints the modified value of the global variable, which is now done by calling the print Global Variable() method.
    • Within the main() function, a new local variable called globalVariable is declared with the value 20. This local variable doesn't have any effect on the global variable of the same name and is only accessible within the main() method.
    • std::endl; \< globalVariable \\ std::cout \\ "New local variable value: " prints the value of the local-global variable, which is 20.






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA