skip to main content
article
Free Access

The detection of dangling references in C++ programs

Published:01 March 1993Publication History
Skip Abstract Section

Abstract

The smart pointer is a programming technique for the C++ language that extends the functionality of the simple pointer. Smart pointers have previously been used to support persistence, distributed objects, reference counting, and garbage collection. This article will show how smart pointers can provide an inexpensive method for detecting dangling pointers to dynamic objects that can be added to any standard C++ implementation.

References

  1. COPLIEN, J. 1992. Advanced C ++ Programming Styles and Idioms. Addison-Wesley, Reading, Mass. Google ScholarGoogle Scholar
  2. EDELSON, D.R. 1992. Precompiling C ++ for garbage collection. In the Internattonal Work-shop on Memory Management (St. Malo, France, Sept. 17-19), INRIA, Le Chesnay, 299-314. Google ScholarGoogle Scholar
  3. EDELSON, D.R. 1992. Smart pointers: They're smart, but they're not pointers. In Usenix C++ Conference Proceedings (Portland, Ore.). Usenix Assoc., Berkeley, Calif., 1-19.Google ScholarGoogle Scholar
  4. FISCHER, C. N. AND LEBLANC, R. J. 1980. The implementation of run-time diagnostics in Pascal. IEEE Trans. Software Eng. SE-6, 4(1980), 313-319.Google ScholarGoogle Scholar
  5. KENNEDY, B. 1991. The features of the object-oriented abstract type hierarchy. In Usenix C++ Conference Proceedings (Washington, D.C.). Usenix Assoc., Berkeley, Calif., 41-50.Google ScholarGoogle Scholar
  6. KERNIGHAN, B. W. AND RITCHIE, D.M. 1988. The C Programming Language. Prentice-Hall, EngleWood Cliffs, N.J. Google ScholarGoogle Scholar
  7. SHAPIRO, M., DICKMAN, P., AND PLAINFOSSE, D. 1992. Robust, distributed references and acyclic garbage collection. In the ACM Sympostum on the Principles of Distributed Computing. ACM, New York. Google ScholarGoogle Scholar
  8. SHAPIRO, M., GOURHANT, Y., HABERT, S., MOSSERO, L., RUFFIN, M., AND VALOT, C. 1989. SOS: An object-oriented operating system--assessment and perspectives. Comput. Syst. 2, 4 287-338.Google ScholarGoogle Scholar
  9. STROUSTRUP, B. 1991. The C++ Programming Language. 2rid ed. Addison-Wesley, Reading, Mass. Google ScholarGoogle Scholar
  10. STROUSTRUP, B. 1987. The Evolution of C ++ 1985 to 1987. In Usenix C++ Workshop Proceedings. Usenix Assoc., Berkeley, Calif., 1-22.Google ScholarGoogle Scholar
  11. WIRTH, N. 1976. In PASCAL Newslett. 5, 29-30.Google ScholarGoogle Scholar

Index Terms

  1. The detection of dangling references in C++ programs

            Recommendations

            Reviews

            Cay Stephan Horstmann

            A technique for implementing so-called smart pointers, which detect dangling references, in C++ is described. Smart pointers are objects of special classes with overloaded operators for dereferencing and structure access; they mimic regular pointers but provide some additional smarts. Typical applications for smart pointers are garbage collection (to retire unused objects) and distributed computing (to access remote objects). The author's technique marks each object with a unique identifying number and replicates that number in all pointers to the object. When the object is deallocated through one pointer, and the memory location is later accessed through one of the other pointers, the dangling reference has a high likelihood of being detected, since the identifying number stored with the pointer is unlikely to match the number that happens to be at the inspected storage location in the heap (which may be inside the free list or another live object). The cost of the technique is obvious. Each pointer, and each object, must carry an added integer. More important, the outlined implementation places the object identifiers in a common base class and requires that all classes that aim to benefit from the detection scheme derive from that class. In practice, that requirement is likely to be cumbersome. The paper gives sufficient detail of the C++ implementation to allow someone skilled in the art of C++ templates and operator overloading to replicate it. The author gives some useful hints on structuring code to facilitate turning the checking on and off when switching between test and release versions. The author suggests that the method can be extended to pointers to constant objects, a common C++ idiom. That is in fact true, but the extension to polymorphic pointers, the cornerstone of virtual dispatch in C++, is known to be much more difficult and far from seamless. Unfortunately, no quantitative measurements of the time and space cost of the scheme are provided.

            Access critical reviews of Computing literature here

            Become a reviewer for Computing Reviews.

            Comments

            Login options

            Check if you have access through your login credentials or your institution to get full access on this article.

            Sign in

            Full Access

            • Published in

              cover image ACM Letters on Programming Languages and Systems
              ACM Letters on Programming Languages and Systems  Volume 2, Issue 1-4
              March–Dec. 1993
              241 pages
              ISSN:1057-4514
              EISSN:1557-7384
              DOI:10.1145/176454
              Issue’s Table of Contents

              Copyright © 1993 ACM

              Publisher

              Association for Computing Machinery

              New York, NY, United States

              Publication History

              • Published: 1 March 1993
              Published in loplas Volume 2, Issue 1-4

              Permissions

              Request permissions about this article.

              Request Permissions

              Check for updates

              Qualifiers

              • article

            PDF Format

            View or Download as a PDF file.

            PDF

            eReader

            View online with eReader.

            eReader
            About Cookies On This Site

            We use cookies to ensure that we give you the best experience on our website.

            Learn more

            Got it!