Using local variables instead of global variable results in better application manageability by reducing or avoiding side effects entirely. Using local variables or parameters reduces global memory usage but increases stack usage.
Target stack access capability influences code quality. Depending on the target capabilities, access to local variables may be very inefficient. Targets without a dedicated stack pointer require the use of an address register instead, making the address register unavailable for other values. Limited offsets or addressing modes causes inefficient variable access as well.
Allocating a large number of local variables causes the Compiler to generate a complex sequence to allocate the stack frame in the beginning of the function and to deallocate it at the end (refer the following listing).
void myfun(void) { /* huge amount of local variables: allocate space! */ /* ... */ /* deallocate huge amount of local variables */ }
If the target provides special entry or exit instructions for such cases, allocation of many local variables is not a problem. You may also use global variables or static local variables. However, this deteriorates maintainability and may waste global address space.
The Compiler may offer an option to overlap parameter or local variables using a technique called overlapping, allocating local variables or parameters as globals. The linker overlaps them depending on their use. For targets with limited stack (for example, no stack addressing capabilities), this often is the only solution. However this solution makes the code non-reentrant (recursion is not allowed).