What is so horrible about global variables?

Previous Memory, C Variables, and Pointers Next

Q: You quote very often in the documentation that we need to avoid global variables as much as possible. What is so wrong with them?
A: Of course, globals are sometimes very necessary, but they are also easy to overuse. The good programming practice is to keep the scope of each identifier as small as possible. But, in addition to conventional programming theory, there are some extra reasons against globals which are related particularly to programming on TI calculators. First, every global variable is translated to the absolute addressing mode. As this mode is non-relocatable, the compiler adds a 4-byte long entry into the relocation table for each apperance of the global variable. If global variable "a" is mentioned say 100 times in the program, this is extra 400 bytes. When I ported some programs from PC to TI, I suceeded to reduce program space from 20K to 16K just by eliminating globals. And 4K of space is quite worth on TI... Second, globals are kept in the .89z (or .9xz) file itself. Sometimes it is good (to keep permanent data which will survive after the program exits etc.), but very often it is a wasting of file space (as the file length is limited). Read questions given further in this document to see how to reduce a file size if you have a strong reasons for using global arrays. To learn: avoid extensive usage of globals as much as possible!