answersLogoWhite

0


Best Answer

The only case where a global variable is advantageous is when that global is a constant variable and it represents a truly global concept. The value of PI, for instance, is a truly global concept and it has a constant value. The exchange rate between dollars and Sterling is also a truly global concept, but it is non-constant and should not be declared global.

Global (non-constant) variables are problematic because it can be difficult to track down all the places that interact with a global, especially if the global has external linkage. Even if that is not a problem in itself, a global cannot cater for both single-threaded and multi-threaded applications. If intended purely for single-threaded applications then there will be no synchronisation mechanism, thus it cannot be used in a multi-threaded application as this could lead to a data race. Conversely, if intended purely for multi-threaded applications, it will have a synchronisation mechanism but this could lead to performance issues on single-threaded applications where synchronisation is not required.

In trivial applications, global variables can often be useful because the scope of a global can be well-defined. But in non-trivial applications, it becomes more difficult to limit the scope of a global. One way to limit the scope is to declare the global variable static, thus limiting its scope to the translation unit in which it is declared (static global variables cannot have external linkage). However, by limiting the scope, the variable is no longer a truly global concept.

User Avatar

Wiki User

8y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What are the five advantages and five disadvantages of using global variables?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What are advantages and disadvantages of using a heligimbal?

what are the advantages and disadvantages of using a heligimbal


What are 2 advantages and 2 disadvantages of using fossil fuels?

Advantages: they're reliable; and they're not expensive. Disadvantages: they're bad for the environment; and the carbon dioxide from burning them is causing global warming.


What 2 advantages and 2 disadvantages of using fossil fuels?

Advantages: they're reliable; and they're not expensive. Disadvantages: they're bad for the environment; and the carbon dioxide from burning them is causing global warming.


What are advantages and disadvantages of using vouchers?

what are the advantages and disadvantages of voucher in business


Advantages and disadvantages of using technology in refrigeration?

what are the advantages and disadvantages of refrigeration


What are the advantages and the disadvantages of using quilting?

what is the advantages and disadvantages of quilted fabric


What are the advantages and disadvantages to using a ring mouse?

advantages and disadvantages of ring mouse


Advantages Disadvantages of using a shaper machine?

what are advantages and disadvantages of shapper machine


What is the advantages and disadvantages of usinf sales figures?

There are great advantages and disadvantages to using sales figures. Advantages to using sales figures is to help with projections of sale for future years. The disadvantages of using sales figures is the market is volatile.


What are the advantages of using a electronic diary?

What are all the advantages and disadvantages of using an electronic dairy?


Advantages kg disadvantages of using inorganic refrigerants?

There are several advantages and disadvantages of using 1 KG inorganic refrigerants. Some of the advantages and disadvantages are cost, energy efficiency, safety issues, and system issues.


When possible you should avoid using variables in a program.?

global