C++

Topic: Variable

Why global variables are evil ?

Global variables should be avoided for several reasons, but the primary reason is because they increase your program’s complexity immensely. For example, say you were examining a program and you wanted to know what a variable named g_nValue was used for. Because g_nValue is a global, and globals can be used anywhere in the entire program, you’d have to examine every single line of every single file! In a computer program with hundreds of files and millions of lines of code, you can imagine how long this would take!Second, global variables are dangerous because their values can be changed by any function that is called, and there is no easy way for the programmer to know that this will happen. Consider the following program:// declare global variableint g_nMode = 1; void doSomething(){    g_nMode = 2;} int main(){    g_nMode = 1;     doSomething();     // Programmer expects g_nMode to be 1    // But doSomething changed it to 2!     if (g_nMode == 1)        cout << "No threat detected." << endl;    else        cout << "Launching nuclear missiles..." << endl;     return 0;}Note that the programmer set g_nMode to 1, and then called doSomething(). Unless the programmer had explicit knowledge that doSomething() was going to change the value of g_nMode, he or she was probably not expecting doSomething() to change the value! Consequently, the rest of main() doesn’t work like the programmer expects (and the world is obliterated).Global variables make every function call potentially dangerous, and the programmer has no easy way of knowing which ones are dangerous and which ones aren’t! Local variables are much safer because other functions can not affect them directly. Consequently, global variables should not be used unless there is a very good reason!

Browse random answers:

Scope of variables
Explain about Initialization of variables ?
Explain about Variable Declaration in C++ ?
Explain about Variable Initialization in C++:
Explain about Initializing Local and Global Variables ?
Explain about Global Variables ?
Explain about Calculations and variables ?
Why global variables are evil ?
How to store letters in a variable?
declaring floating-point type variables
What are variable declaration in c++ syntax ?
Write a function using reference variables as arguments to swap the values of a pair of integers?
How to access change a variable using pointer in C++?
Write a program to declare two integer , one float variables and assign 10, 15, and 12.6 to them respectively. It then prints these values on the screen.
What is a Variable?
What is an algorithm (in terms of the STL/C++ standard library)?
what are auto static variables and auto extern variables?
How many ways are there to initialize an int with a constant?
How do you decide which integer type to use?
How is static variable stored in the memory?( if there are 2 functions in a file,and the static variable name is same (ex var) in both the function. how is it keep seperately in the memory).
What can I safely assume about the initial values of variables which are not explicitly initialized?
What?s the auto keyword good for?
What is the difference between declaration and definition?
How variable declaration in c++ differs that in c?
What is difference between followin intialization.int iVar1;int iVar2 = int();and which one of two should we prefer always and why?
Are private class-level variables inherited?
Are there any new intrinsic (built-in) data types?
I was trying to use an "out int" parameter in one of my functions. How should I declare the variable that I am passing to it?
When you inherit a protected class-level variable, who is it available to?
What is a node class in c++?
What is the difference between an external iterator and an internal iterator? Describe an advantage of an external iterator.
Explain about Variables Data Types ?
Explain about Fundamental data types ?
Explain about Declaration of variables ?