Browse Prior Art Database

Smart pointer usage for C structures and primitive datatypes

IP.com Disclosure Number: IPCOM000014933D
Original Publication Date: 2001-Jul-30
Included in the Prior Art Database: 2003-Jun-20
Document File: 2 page(s) / 71K

Publishing Venue

IBM

Abstract

The C++ language introduces many features and capabilities that provide a great deal of flexibility. Furthermore, when a programmer uses these features properly, there will be an increase in the quality, readability, and maintainability of their code. Ultimately, some of these C++ features and their direct benefits can be useful in proving the correctness of a C++ program. This proof of correctness would guarantee that the number of defects discovered in the product and thus the costs of servicing the generally available product would be minimized considerably. The most notable C++ features are: compatibility with the C programming language, operator overloading, templates, exceptions, and object-oriented constructs such as classes, dynamic dispatching, and polymorphism. Unfortunately, some of these features come with caveats. In particular, the use of exceptions increases the possibility of never de-allocating storage that was explicitly allocated. This error is commonly known as a memory leak and can result in resource exhaustion. The use of exceptions are further complicated when abstract data types defined using C structures coexist with C++ classes. A general-purpose solution is defined here that allows the a team to take advantage of the C++ language while avoiding the pitfalls that come when these features are not used properly.

This text was extracted from a PDF file.
This is the abbreviated version, containing approximately 52% of the total text.

Page 1 of 2

Smart pointer usage for C structures and primitive datatypes

The C++ language introduces many features and capabilities that provide a great deal of flexibility. Furthermore, when a programmer uses these features properly, there will be an increase in the quality, readability, and maintainability of their code. Ultimately, some of these C++ features and their direct benefits can be useful in proving the correctness of a C++ program. This proof of correctness would guarantee that the number of defects discovered in the product and thus the costs of servicing the generally available product would be minimized considerably. The most notable C++ features are: compatibility with the C programming language, operator overloading, templates, exceptions, and object-oriented constructs such as classes, dynamic dispatching, and polymorphism. Unfortunately, some of these features come with caveats. In particular, the use of exceptions increases the possibility of never de-allocating storage that was explicitly allocated. This error is commonly known as a memory leak and can result in resource exhaustion. The use of exceptions are further complicated when abstract data types defined using C structures coexist with C++ classes. A general-purpose solution is defined here that allows the a team to take advantage of the C++ language while avoiding the pitfalls that come when these features are not used properly.

C++ templates are used to help solve the problem of memory leaks. Template classes are used to avoid memory leaks, while providing the opportunity for other advanced features such as reference counting. When used in this fashion, these template classes are commonly known as Smart Pointers. Currently many class libraries including the Standard Template Library (STL) and the IBM Open Class Library (IOC) provide template definitions to be instantiated as Smart Pointers. This concept is extended here to primitive data types and abstract data types defined using C structures to instantiate each class library's Smart Pointer templates. In the past, compilation could fail or memory leaks could still occur when not using C++ classes to instantiate a Smart Pointer. Further, these implementations do not allow for programmers to use Smart Pointers to handle other problems that may arise when an exception is thrown. This generic programming framework accomplishes the following:

* Provides (not reference counted) Smart Pointer template classes that: o Use type traits to correctly de-allocate all explicitly allocated storage for:

      C++ class instances Abstract data types defined using C structures...