C++ const and Immutability: An Empirical Study of Writes-Through-const

Authors Jon Eyolfson, Patrick Lam

Thumbnail PDF


  • Filesize: 0.58 MB
  • 25 pages

Document Identifiers

Author Details

Jon Eyolfson
Patrick Lam

Cite AsGet BibTex

Jon Eyolfson and Patrick Lam. C++ const and Immutability: An Empirical Study of Writes-Through-const. In 30th European Conference on Object-Oriented Programming (ECOOP 2016). Leibniz International Proceedings in Informatics (LIPIcs), Volume 56, pp. 8:1-8:25, Schloss Dagstuhl – Leibniz-Zentrum für Informatik (2016)


The ability to specify immutability in a programming language is a powerful tool for developers, enabling them to better understand and more safely transform their code without fearing unintended changes to program state. The C++ programming language allows developers to specify a form of immutability using the const keyword. In this work, we characterize the meaning of the C++ const qualifier and present the ConstSanitizer tool, which dynamically verifies a stricter form of immutability than that defined in C++: it identifies const uses that are either not consistent with transitive immutability, that write to mutable fields, or that write to formerly-const objects whose const-ness has been cast away. We evaluate a set of 7 C++ benchmark programs to find writes-through-const, establish root causes for how they fail to respect our stricter definition of immutability, and assign attributes to each write (namely: synchronized, not visible, buffer/cache, delayed initialization, and incorrect). ConstSanitizer finds 17 archetypes for writes in these programs which do not respect our version of immutability. Over half of these seem unnecessary to us. Our classification and observations of behaviour in practice contribute to the understanding of a widely-used C++ language feature.
  • empirical study
  • dynamic analysis
  • immutability


  • Access Statistics
  • Total Accesses (updated on a weekly basis)
    PDF Downloads


  1. Shay Artzi, Adam Kiezun, David Glasser, and Michael D. Ernst. Combined static and dynamic mutability analysis. In ASE, pages 104-113, November 2007. Google Scholar
  2. Derek Bruening and Qin Zhao. Practical memory checking with Dr. Memory. In CC, pages 213-223, 2011. Google Scholar
  3. David Chisnall, Colin Rothwell, Robert N. M. Watson, Jonathan Woodruff, Munraj Vadera, Simon W. Moore, Michael Roe, Brooks Davis, and Peter G. Neumann. Beyond the PDP-11: architectural support for a memory-safe C abstract machine. In ASPLOS, 2015. Google Scholar
  4. Felix Fang. Personal communication, 2015. Google Scholar
  5. Wei Huang, Ana Milanova, Werner Dietl, and Michael D. Ernst. ReIm & ReImInfer: Checking and inference of reference immutability and method purity. In OOPSLA, 2012. Google Scholar
  6. ISO. Programming languages - C++. N3690, May 2013. Google Scholar
  7. Scott Meyers. Effective C++: 55 Specific Ways to Improve Your Programs and Designs. Addison Wesley, 3rd edition, 2005. Google Scholar
  8. Stephen Nelson, David J. Pearce, and James Noble. Profiling field initialisation in Java. In RV, volume 7687 of LNCS, pages 292-307, 2012. URL: http://dx.doi.org/10.1007/978-3-642-35632-2_28.
  9. Alex Potanin, Johan Östlund, Yoav Zibin, and Michael D. Ernst. Immutability. In Aliasing in Object-Oriented Programming. Types, Analysis and Verification, volume 7850 of LNCS, pages 233-269. 2013. Google Scholar
  10. Lukas Rytz, Nada Amin, and Martin Odersky. A flow-insensitive, modular effect system for purity. In FTFJP, July 2013. Google Scholar
  11. Alexandru Salcianu. Pointer Analysis for Java Programs: Novel Techniques and Applications. PhD thesis, MIT, 2006. Google Scholar
  12. Alexandru Salcianu and Martin C. Rinard. Purity and side effect analysis for Java programs. In VMCAI, pages 199-215, January 2005. Google Scholar
  13. Konstantin Serebryany, Derek Bruening, Alexander Potapenko, and Dmitriy Vyukov. AddressSanitizer: A fast address sanity checker. In USENIX Annual Technical Conference, pages 309-318, 2012. Google Scholar
  14. Evgeniy Stepanov and Konstantin Serebryany. MemorySanitizer: Fast detector of uninitialized memory use in C++. In CGO, pages 46-55, 2015. Google Scholar
  15. Herb Sutter. GotW #6a solution: Const-correctness, part 1. http://herbsutter.com/2013/05/24/gotw-6a-const-correctness-part-1-3/, May 2013. Accessed Dec 2015.
  16. LLVM Team. The LLVM compiler infrastructure. http://llvm.org/, December 2015.
  17. Matthew S. Tschantz. Javari: Adding reference immutability to Java. Master’s thesis, Massachusetts Institute of Technology, 2006. Google Scholar
  18. Matthew S. Tschantz and Michael D. Ernst. Javari: Adding reference immutability to Java. In OOPSLA, pages 211-230, October 2005. Google Scholar
  19. Christopher Unkel and Monica S. Lam. Automatic inference of stationary fields: a generalization of Java’s final fields. In POPL, pages 183-195, January 2008. Google Scholar
  20. Qin Zhao, Derek Bruening, and Saman P. Amarasinghe. Umbra: Efficient and scalable memory shadowing. In CC, pages 22-31, 2010. Google Scholar