You are on page 1of 2

Modern C++ Design

“Smart Pointers“
Sommercampus '05
1. Implement a Smart Pointer

Download the tar-ball from the Sommercampus homepage and un-tar it into your working
directory. Compile and run the program exercise01. It shouldn't output anything.

Now check the program using valgrind's memory checker:

$ setenv GLIBCPP_FORCE_NEW 1
$ setenv MALLOC_CHECK_ 0
$ valgrind --tool=memcheck --leak-check=full \
--show-reachable=yes ./exercise01

Obviously the program is less than optimal. Have a look at the source of the program. At the top
of the file the type StrPtr is defined. You can alternatively define it as a Smart Pointer (by
#define'ing SMARTPTR). The Smart Pointer class however is still incomplete. Flesh it out, so
that it becomes an unchecked, reference counted Smart Pointer.

Compile & run the program again. It should still output no error. Run it with valgrind again.
If your Smart Pointer is working correctly, there shouldn't be any errors left.

What is the meaning of the two environment variables ( GLIBCPP_FORCE_NEW &


MALLOC_CHECK_)?

2. Get better used to template's

Our Smart Pointer class is still not very close to raw pointers. Copy your Smart Pointer template
into the file smartptr.h and add the comparison operators. At the top of the file you will
find a list of operators needed. (see the file hint1 if you don’t remember (and can’t find) the
syntax of the comparison operators, but please first try it yourself)

We will also need templated versions of the operators. Remember why? Compile the program
exercise02 and see. Add the templated versions in smartptr.h. Do we really need the
non-templated versions as well? Comment them out, and try to compile exercise02.cc. (see
hint2 for hints on this, after trying it yourself!)

The program still shouldn't compile. The compiler has problems comparing two Smart Pointers
with each other. Implement the operator templates needed for that comparison. How can you
access the pointee_ of the other Smart Pointer? Implement an accessor GetImpl() for that.
(hint3 offers some help here)

With both, the normal and all the templated operators, you should be able to compile and run
exercise02 now.
extra: The last assert() in the source is commented out. It would work with a normal
pointer, can you make it work with your Smart Pointer? What would happen, if you
delete c then?

3. Use your Smart Pointers

Execute make in the top-level directory. This should build libbdd.a in the sub-directory
libbdd/ and the binary qsat in the sub-directory qsat. qsat is a small solver for first-order
theory with Boolean variables, see the file qsat.y for the EBNF grammar it accepts as input,
i.e.:

exists ( x . (x or y or not z) and (y or z) and (not x and z))

There are some test-cases in the qsat/ sub-directory, execute them with make test. When
you run qsat with valgrind, however, you will find that it leaks some memory. This is due
the simple BDD (you should know those from TI 1) library, which does not implement any
means of reference checking/memory management. Have a look at the file bdd.h, it already
contains some hooks for your smartptr.h. Uncomment them and compile both, the library and
the solver, with your new version.

It probably doesn’t work at your first attempt, most probably because your smart pointer isn’t
yet ready to be used with STL containers. (if you need help with this, see hint4, but first try to
fix it yourself)

When the solver compiles with your smart pointer, it shouldn’t leak memory any more. With
only changing the pointer type for BDD nodes from normal to smart pointers, you added
reference counting and memory management to the library!

There is also a more simple application of the BDD library in the subdirectory test/, so you
can play around a little with the BDD library. Also have a look at the Makefiles and the
lex/yacc files in the qsat/ directory – they might come in handy one day as templates for
something.

You might also like