answersLogoWhite

0


Best Answer

The term variables imply that those things may be changed or assigned with new value (at numerous places). This make the program harder to debug because it will be difficult to know when and where a global variable being changed and what was the consequence of that value changed.

User Avatar

Wiki User

13y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

8y ago

Globals should only be used when an object represents a truly global concept; some entity that needs to be widely accessible by your program. The standard input and output streams are examples of truly global concepts.

The main problem with globals is that they are generally unsuitable for multi-threaded applications where one thread writes to a global while another thread is concurrently reading that same global. The same problem can occur when two threads attempt to read and write shared memory, even if that memory is localised. Local variables do not have this problem, however global variables and shared memory must be synchronised so that only one thread gains access at a time, blocking other threads until the resource is unlocked. This adds a runtime overhead that can often be eliminated by using local variables, particularly when the variables are trivial to copy.

This answer is:
User Avatar

User Avatar

Wiki User

9y ago

A global variable is any named variable declared outside of any function (in the global namespace). Any code that can "see" the declaration or that has external linkage to the name can potentially alter the global's value. This can make it difficult to determine when and where the value is being changed and for what reason, especially where the mutation is occurring within code that is not accessible to the programmer.

Code becomes much easier to maintain by localising variables and limiting access to those variables. That is, when a function needs to access a value, it should either use the pass by value semantic or the pass by constant reference semantic. If the function needs to mutate the value itself, it should use the pass by reference semantic, however the function should clearly document how the reference will be mutated and for what reason.

Although global variables are generally frowned upon, they do have their uses, particularly in trivial programs. However, even complex programs can make use of them provided they truly represent a global concept. For instance, standard input and standard output are global concepts, thus it makes sense to define them as such. However, we must be careful when using globals in multi-threaded applications, especially if the global has no synchronisation mechanism in place. While there is no problem with multiple threads accessing a global, if any thread mutates a global while it is being accessed by another thread, or if multiple threads attempt to concurrently mutate a global, our program has undefined behaviour. For instance, if two threads attempt to concurrently write to stdout without using synchronisation, the output may be garbled.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Why do global variables make a program difficult to debug?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

How do you debug a program in visual basic?

Press the debug icon.


What is the process of testing and eliminating errors in a program?

debug debug


What program removes glitches?

debug


How can you debug a C program in Unix?

You can debug C programs using gdb on Unix.


What is computer program which removes glitches?

debug


How do you debug joomla?

Login to the Joomla backend: - Go to site->Global configuration->System and change the Debug System on the right to true.


What are the advantages and disadvantages of machine language?

AdvantageThe only advantage is that program of machine language run very fast because no translation program is required for the CPU.DisadvantagesIt is very difficult to program in machine language. The programmer has to know details of hardware to write program.The programmer has to remember a lot of codes to write a program which results in program errors.It is difficult to debug the program.


Code View Debug info use in vb 6?

The program should have a help area. search the helpe area for information about the debug information the program displays/


What is the sort cut for running c plus plus program?

It depends on the particular IDE. Visual Studio uses <Ctrl>F5 to start a program in non-debug mode, and F5 to start a program in debug mode.


What does this mean and how do i remove it Just in Time debugging However no debuggers are registered that can debug this exception Unable to JIT debug?

You are attempting to run a debug version of a machine code program, but you do not have the required debugger.


What is the full form of MP LAB?

Make program debug/executive


What is difference between design and debug visual basic?

Design - Placing object on the form - Buttons, Textboxes.... Debug - The program actually runs the code.