Safe C++

This review was originally printed in the September 2012 edition of the UKUUG newsletter, which is now defunct.

Title: Safe C++
Authors: Vladimir Kushnir
ISBN: 9781449321369

Given the relatively low level at which the C++ language operates, there are plenty of opportunities to make mistakes (e.g. in memory management) which rarely come up in other languages. This book attempts to cover some of the most common mistakes, and to provide strategies for preventing or working around them.

The first chapter is remarkably brief – just two pages on where bugs come from, including common mistakes in C and the abusable features added by C++. Chapter two discusses the importance of catching bugs early, and demonstrates ways to refactor code so that the compiler can spot potential errors, such as by using the explicit keyword and using enums to create new types. However, the author notes that most bugs arise as a result of data which is not known at compile time – such as that supplied by users – and therefore the rest of the book covers only runtime errors.

Chapter 3 covers what to do when encountering a runtime error, which is arguably as important as detecting the error in the first place. A helpful macro is provided for printing useful error messages. The next chapter comprehensively covers the ‘index out of bounds’ bug, with sections on dynamic, static and multidimensional arrays. Chapter five is half a page and basically states, in several different ways, ‘do not use pointer arithmetic’ (a source of many bugs and unreadable code). Sound advice, although perhaps difficult to apply to existing software projects.

The next two chapters skim over invalid pointers and uninitialised variables. Whilst most of the advice is sensible, the author ends up recommending the creation of wrapper classes for every basic type (e.g. int becomes Int), and to use these for declaring all class members, which seems an unnecessary level of abstraction. Memory leaks and NULL pointers are given a chapter each, though I think both topics could benefit from greater coverage.

The final part of the book covers testing and debugging. Most of this is at a fairly high level, but there is some useful advice on making code ‘debugger friendly’. There are several appendices, but all are source code listings and feel as if they are padding out the book, since the code can be downloaded from the publisher’s website.

Overall, for such a small book (88 pages if you ignore the appendices), this does a good job of covering the basic C++ mistakes to look out for, and how to write code which is more resiliant to begin with. The book covers the subject area in breadth rather than depth, so further reading may be required on several of the topics in order to gain a full understanding, but otherwise this is a useful text for anyone who writes C++ on a regular basis.

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.