Source-to-Source Refactoring and Elimination of Global Variables in C Programs

Hemaiyer Sankaranarayanan, Prasad A. Kulkarni
2013 Journal of Software Engineering and Applications  
A global variable in C/C++ is one that is declared outside a function, and whose scope extends the lifetime of the entire program. Global variables cause problems for program dependability, maintainability, extensibility, verification, and thread-safety. However, global variables can also make coding more convenient and improve program performance. We have found the use of global variables to remain unabated and extensive in real-world software. In this paper we present a source-to-source
more » ... oring tool to automatically detect and localize global variables in a program. We implement a compiler based transformation to find the best location to redefine each global variable as a local. For each global, our algorithm initializes the corresponding new local variable, passes it as an argument to necessary functions, and updates the source lines that used the original global to now instead use the corresponding local or argument. In this work we also characterize the use of global variables in common benchmark programs. We study the effect of our transformation on static program properties, such as the change in the number of function arguments and program state visibility. Additionally, we quantify dynamic program characteristics, including memory and runtime performance, before and after our localizing transformation.
doi:10.4236/jsea.2013.65033 fatcat:vdftwu5zsvbnbooqpu44mjmlpi