answersLogoWhite

0


Want this question answered?

Be notified when an answer is posted

Add your answer:

Earn +20 pts
Q: Why having many global variables is a bad idea?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Why are you having so many earthquakes in this decade?

Global warming


Why global variables need to be kept to a minimum?

Polluting the global namespace with variables that have no reason to be global is never a good idea. Aside from anything else, the programmer has no control over what code may access a global, and global variable declarations may be spread over many files or hidden within headers, making it difficult to get an overview of every global. Globals are not necessarily a bad thing; if a concept is global in nature then of course make it global. But don't make variables global in the mistaken belief that it is somehow convenient. If a variable really needs to be shared amongst a specific set of functions then there are far better ways of doing so without making the variable global to all functions. Declaring the variable locally in a function (even the main function as a static variable) and passing it to the functions that operate upon it, or by encapsulating the variable in a class along with the functions that operate upon it are far better options than making a variable globally accessible. Keeping variables as close to the code that operates upon them makes your code easier to both read and maintain.


What are local variables and global variables?

Local variables: These variables only exist inside the specific function that creates them. They are unknown to other functions and to the main program. As such, they are normally implemented using a stack. Local variables cease to exist once the function that created them is completed. They are recreated each time a function is executed or called. Global variables: These variables can be accessed (ie known) by any function comprising the program. They are implemented by associating memory locations with variable names. They do not get recreated if the function is recalled.


What are the different storage class in c?

The four storage classes in C are: automatic, static, external and register. Note that storage classes are not classes in the object-oriented programming sense, they simply define the scope (visibility) of a variable.Automatic Variables (auto)All local variables are automatic by default so we seldom see the auto keyword in code. Local variables are variables declared at function scope.Static Variables (static)All global variables are static by default. Global variables are variables declared at file scope (outside of any function). Static variables can also be explicitly declared inside functions to override the default automatic storage class. All static variables, whether global or local, are allocated within the program's data segment (static memory) and do not fall from scope even if declared locally. All static variables are initialised to zero by default.It's best to avoid the use of global variables unless they are declared constant (const) as it can be difficult to keep track of all the places where a global variable is being operated upon (accessed or assigned to). This can lead to data races in multi-threaded applications unless we take steps to synchronise all access and assignment operations upon the variable. For that reason it's best to keep variables as localised as possible, passing arguments into functions whenever we need to cross scopes. Non-constant global variables should really only be considered if they truly represent a global concept within the file in which they are declared.External Variables (extern)External storage can only be applied to a global variable declared outwith file scope. That is, when a global variable is declared in one file, any external file can gain access to that same global variable simply by declaring the same name and type but with external storage. It follows that external variables are also static variables and is the only case where a variable has two storage classes. Note the local static variables (including local constant variables) cannot be declared external, they are local to the function in which they are declared.This is another reason why it is best to avoid using too many global variables. While we can generally keep track of which code can access a global variable at file scope we have no means of limiting access from outwith that file. Again, prefer local variables to global variables whenever possible.Register Variables (register)A register variable is a variable that we wish to allocate to a CPU register rather than in RAM. Register variables must be no larger than the word-length of the machine and should only be used when we explicitly require fast access to the variable, such as loop counters, accumulators and pointer variables. Note that CPU registers have no address (no identity we can refer to) so we cannot use the unary '&' operator to take the address of a register variable. This means we cannot use pointers to refer to them indirectly which, in turn, means we can only pass them to functions by value (not by reference). However, to do so would defeat the purpose of using the register storage class.Given the limited number of registers available, there is no guarantee that a register variable will actually be allocated to a register; the register keyword is merely a hint to the compiler. It should be noted that modern compilers are extremely good at optimising code so there is seldom any need to explicitly declare register variables.


What is the difference between a global and a multidomestic approach to business?

A global approach to business focuses on having a business in many nations. A multi-domestic approach to business means that a business has many businesses in one nation.


How many variables are tested during an experiment?

There are three types of variables tested: manipulated variables, controlled variables, and experimental variables.


Why the rainforest is having a effect on global warming?

I dont know, i have been to this page for many tipe of questions like that and no answers


How many variables an 8-square eliminates?

3 variables


How many variables do formulas have?

There is no limit to the number of variables a formula can have.


How do you make a baby in Minecraft?

There have many ways to create it. you have to search in google for having great idea.


What is having too many electrons or too few electrons?

I have this question too.... but i have no idea what the answer is sorryyy :*


When was the idea of global warming thought up?

well scientist knew about it 30 years before but however did not have any evidence on there are many sceptics now a days how still don't believe global warming is man made!