Skip to main content

Coping with Type Casts in C

  • Conference paper
  • First Online:
Software Engineering — ESEC/FSE ’99 (ESEC 1999, SIGSOFT FSE 1999)

Abstract

The use of type casts is pervasive in C. Although casts provide great flexibility in writing programs, their use obscures the meaning of programs, and can present obstacles during maintenance. Casts involving pointers to structures (C structs) are particularly problematic, because by using them, a programmer can interpret any memory region to be of any desired type, thereby compromising C’s already weak type system.

This paper presents an approach for making sense of such casts, in terms of understanding their purpose and identifying fragile code. We base our approach on the observation that casts are often used to simulate object-oriented language features not supported directly in C. We first describe a variety of ways — idioms — in which this is done in C programs. We then develop a notion of physical subtyping, which provides a model that explains these idioms.

We have created tools that automatically analyze casts appearing in C programs. Experimental evidence collected by using these tools on a large amount of C code (over a million lines) shows that, of the casts involving struct types, most (over 90%) can be associated meaningfully — and automatically — with physical subtyping. Our results indicate that the idea of physical subtyping is useful in coping with casts and can lead to valuable software productivity tools.

This is a preview of subscription content, log in via an institution to check access.

Access this chapter

Chapter
USD 29.95
Price excludes VAT (USA)
  • Available as PDF
  • Read on any device
  • Instant download
  • Own it forever
eBook
USD 39.99
Price excludes VAT (USA)
  • Available as PDF
  • Read on any device
  • Instant download
  • Own it forever
Softcover Book
USD 54.99
Price excludes VAT (USA)
  • Compact, lightweight edition
  • Dispatched in 3 to 5 business days
  • Free shipping worldwide - see info

Tax calculation will be finalised at checkout

Purchases are for personal use only

Institutional subscriptions

Preview

Unable to display preview. Download preview PDF.

Unable to display preview. Download preview PDF.

References

  1. Martín Abadi and Luca Cardelli. A Theory of Objects. Springer, 1996.

    Google Scholar 

  2. Luca Cardelli. A semantics of multiple inheritance. In G. Kahn, D.B. MacQueen, and G. Plotkin, editors, Semantics of Data Types, number 173 in Lecture Notes in Computer Science, pages 51–68. Springer-Verlag, 1984.

    Chapter  Google Scholar 

  3. Luca Cardelli and Peter Wegner. On understanding types, data abstraction, and polymorphism. Computing Surveys, 17(4):471–522, December 1985.

    Article  Google Scholar 

  4. David Evans. Static detection of dynamic memory errors. In Proceedings of the ACM SIGPLAN’96 Conference on Programming Language Design and Implementation, pages 44–53, May 1996.

    Google Scholar 

  5. F.-J. Grosch and G. Snelting. Polymorphic components for monomorphic languages. In R. Prieto-Diaz and W.B. Frakes, editors, Advances in Software Reuse: Selected Papers from the Second International Workshop on Software Reusability, pages 47–55, Lucca, Italy, March 1993. IEEE Computer Society Press.

    Chapter  Google Scholar 

  6. Carl A. Gunter. Semantics of Programming Languages. The MIT Press, 1992.

    Google Scholar 

  7. S. C. Johnson. Lint, a C program checker, July 1978.

    Google Scholar 

  8. S. C. Johnson and D. M. Ritchie. UNIX time-sharing system: Portability of C programs and the UNIX system. Bell Systems Technical Journal, 57(6):2021–2048, 1978.

    Article  Google Scholar 

  9. Robert O’Callahan and Daniel Jackson. Lackwit: A program understanding tool based on type inference. In 19th International Conference on Software Engineering (ICSE), pages 338–48, May 1997.

    Google Scholar 

  10. M. Siff, S. Chandra, T. Ball K. Kunchithapadam, and T. Reps. Coping with type casts in c. Technical Report BL0113590-990202-03, Lucent Technologies, Bell Laboratories, February 1999.

    Google Scholar 

  11. Michael Siff and Thomas Reps. Program generalization for software reuse: From C to C++. In Fourth ACM SIGSOFT Symposium on the Foundations of Software Engineering, pages 135–146, San Francisco, October 1996.

    Google Scholar 

  12. Geoffrey Smith and Dennis Volpano. Towards an ML-style polymorphic type system for C. In 1996 European Symposium on Programming, April 1996.

    Google Scholar 

  13. Bjarne Steensgaard. Points-to analysis by type inference of programs with structures and unions. In Proceedings of the 1996 International Conference on Compiler Construction, number 1060 in Lecture Notes in Computer Science, pages 136–150. Springer-Verlag, April 1996.

    Google Scholar 

Download references

Author information

Authors and Affiliations

Authors

Editor information

Editors and Affiliations

Rights and permissions

Reprints and permissions

Copyright information

© 1999 Springer-Verlag Berlin Heidelberg

About this paper

Cite this paper

Siff, M., Chandra, S., Ball, T., Kunchithapadam, K., Reps, T. (1999). Coping with Type Casts in C. In: Nierstrasz, O., Lemoine, M. (eds) Software Engineering — ESEC/FSE ’99. ESEC SIGSOFT FSE 1999 1999. Lecture Notes in Computer Science, vol 1687. Springer, Berlin, Heidelberg. https://doi.org/10.1007/3-540-48166-4_12

Download citation

  • DOI: https://doi.org/10.1007/3-540-48166-4_12

  • Published:

  • Publisher Name: Springer, Berlin, Heidelberg

  • Print ISBN: 978-3-540-66538-0

  • Online ISBN: 978-3-540-48166-9

  • eBook Packages: Springer Book Archive

Publish with us

Policies and ethics