JSEA  Vol.6 No.5 , May 2013
Source-to-Source Refactoring and Elimination of Global Variables in C Programs
ABSTRACT

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 refactoring 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 global to now instead use the corresponding local or argument. We also characterize the use of global variables in standard benchmark programs. We study the effect of our transformation on static program properties, such as change in the number of function arguments and program state visibility. Additionally, we quantify dynamic program features, including memory and runtime performance, before and after our localizing transformation.


Cite this paper
H. Sankaranarayanan and P. Kulkarni, "Source-to-Source Refactoring and Elimination of Global Variables in C Programs," Journal of Software Engineering and Applications, Vol. 6 No. 5, 2013, pp. 264-273. doi: 10.4236/jsea.2013.65033.
References
[1]   B. W. Kernighan and D. M. Ritchie, “The C Programming Language,” Prentice-Hall, Inc., Upper Saddle River, 1978.

[2]   W. Wulf and M. Shaw, “Global Variable Considered Harmful,” ACM SIGPLAN Notices, Vol. 8, No. 2, 1973, pp. 28-34.

[3]   G. Klein, K. Elphinstone, G. Heiser, J. Andronick, D. Cock, P. Derrin, D. Elkaduwe, K. Engelhardt, R. Kolanski, M. Norrish, T. Sewell, H. Tuch and S. Winwood, “SEL4: Formal Verification of an OS Kernel,” Proceedings of the ACM SIGOPS 22nd Symposium on Operating Systems Principles, SOSP’09, New York, 11-14 October 2009, pp. 207-220,

[4]   J, Barnes, “High Integrity Software: The SPARK Approach to Safety and Security,” Addison-Wesley Longman Publishing Co., Inc., Boston, 2003.

[5]   D. Binkley, M. Harman, Y. Hassoun, S. Islam and Z. Li, “Assessing the Impact of Global Variables on Program Dependence and Dependence Clusters,” Journal of Systems and Software, Vol. 83, No. 1, 2010, pp. 96-107. doi:10.1016/j.jss.2009.03.038

[6]   F. Balmas, “Using Dependence Graphs as a Support to Document Programs,” Proceedings of the Second IEEE International Workshop on Source Code Analysis and Manipulation, SCAM’02, Montreal, 1 October 2002, pp. 45-154

[7]   Y. Deng, S. Kothari and Y. Namara, “Program Slice Browser,” Proceedings of the 9th International Workshop on Program Comprehension, Toronto, 12-13 May 2001, pp. 50-59.

[8]   S. Black, “Computing Ripple Effect for Software Maintenance,” Journal of Software Maintenance, Vol. 13, No. 4, 2001, pp. 263-279. doi:10.1002/smr.233

[9]   P. Tonella, “Using a Concept Lattice of Decomposition Slices for Program Understanding and Impact Analysis,” IEEE Transactions on Software Engineering, Vol. 29, No. 6, 2003, pp. 495-509. doi:10.1109/TSE.2003.1205178

[10]   A. R. Smith and P. A. Kulkarni, “Localizing Globals and Statics to Make C Programs Thread-Safe,” Proceedings of the 14th International Conference on Compilers, Architectures and Synthesis for Embedded System, New York, 2011, pp. 205-214.

[11]   G. Zheng, S. Negara, C. L. Mendes, L. V. Kale and E. R. Rodrigues, “Automatic Handling of Global Variables for Multi-Threaded mpi Programs,” IEEE 17th International Conference on Parallel and Distributed Systems (ICPADS), Tainan, 7-9 December 2011, pp. 220-227.

[12]   P. Sestoft, “Replacing Function Parameters by Global Variables,” Proceedings of the Fourth International Conference on Functional Programming Languages and Computer Architecture, FPCA’89, London, 11-13 September 1989, pp. 39-53.

[13]   B. W. Kernighan, “The C Programming Language,” 2nd Edition, Prentice Hall Professional Technical Reference, 1988.

[14]   c2.com Wiki Contributors, “Global Variables Are Bad,” 2011. http:// c2.com/cgi/wiki?GlobalVari ablesAreBad

[15]   E. Gamma, R. Helm, R. Johnson and J. Vlissides, “Design Patterns: Elements of Reusable Object-Oriented Software,” Addison-Wesley Longman Publishing Co., Inc., Boston, 1995.

[16]   M. Hevery, “Clean Code Talks—Global State and Singletons,” Google Tech Talks, 2008.

[17]   R. E. Sward and A. T. Chamillard, “Re-Engineering Global Variables in Ada,” Proceedings of the 2004 Annual ACM SIGAda International Conference on Ada: The Engineering of Correct and Reliable Software for Real-Time & Distributed Systems Using Ada and Related Technologies, SIGAda’04, Atlanta, 14 November 2004, pp. 29-34.

[18]   X. J. Yang, N. Cooprider and J. Regehr, “Eliminating the Call Stack to Save Ram,” Proceedings of the 2009 ACM SIGPLAN/SIGBED Conference on Languages, Compilers, and Tools for Embedded Systems, LCTES’09, Dublin, 19-20 June 2009, pp. 60-69.

[19]   R. C. Martin, “Clean Code: A Handbook of Agile Software Craftsmanship,” 1st Edition, Prentice Hall PTR, Upper Saddle River, 2008.

[20]   J. Kerievsky, “Refactoring to Patterns,” Pearson Higher Education, 2004.

[21]   D. Wilking, U. F. Khan and S. Kowalewski, “An Empirical Evaluation of Refactoring,” e-Informatica Software Engineering Journal, Vol. 1, No. 1, 2007, pp. 27-42.

[22]   T. Lengauer and R. E. Tarjan, “A Fast Algorithm for Finding Dominators in a Flowgraph,” ACM Transactions on Programming Language Systems, Vol. 1, No. 1, 1979, pp. 121-141. doi:10.1145/357062.357071

[23]   A. V. Aho, M. S. Lam, R. Sethi and J. D. Ullman, “Compilers: Principles, Techniques, and Tools,” Addison-Wesley Longman Publishing, Boston, 2006.

[24]   C. Lattner and V. Adve, “Llvm: A Compilation Framework for Lifelong Program Analysis & Transformation,” Proceedings of the International Symposium on Code Generation and Optimization: Feedback-Directed and Runtime Optimization, CGO’04, IEEE Computer Society, Washington DC, 2004, p. 75

[25]   Clang Team, “Clang: A C Language Family Frontend for Llvm,” 2012. http://clang.llvm.org/

[26]   A. Milanova, A. Rountev and B. G. Ryder, “Precise Call Graphs for C Programs with Function Pointers,” Automated Software Engineering, Vol. 11, No. 1, 2004, pp. 7-26. doi:10.1023/B:A USE.00000 08666.56394.a1

[27]   M. R. Guthaus, J. S. Ringenberg, D. Ernst, T. M. Austin, T. Mudge and R. B. Brown, “MiBench: A Free, Commercially Representative Embedded Benchmark Suite,” IEEE 4th Annual Workshop on Workload Characterization, Austin, December 2001.

[28]   “Standard Performance Evaluation Corporation (SPEC),” 2006. http://www.spec.org/benchma rks.html

 
 
Top