Astute readers such as yourself may be wondering whether the title of this book, Safe
C++, presumes that the C++ programming language is somehow unsafe. Good catch!
That is indeed the presumption. The C++ language allows programmers to make all
kinds of mistakes, such as accessing memory beyond the bounds of an allocated array,
or reading memory that was never initialized, or allocating memory and forgetting to
deallocate it. In short, there are a great many ways to shoot yourself in the foot while
programming in C++, and everything will proceed happily along until the program
abruptly crashes, or produces an unreasonable result, or does something that in computer
literature is referred to as “unpredictable behavior.” So yes, in this sense, the
C++ language is inherently unsafe.
This book discusses some of the most common mistakes made by us, the programmers,
in C++ code, and offers recipes for avoiding them. The C++ community has developed
many good programming practices over the years. In writing this book I have collected
a number of these, slightly modified some, and added a few, and I hope that this collection
of rules formulated as one bug-hunting strategy is larger than the sum of its parts.
The undeniable truth is that any program significantly more complex than “Hello,
World” will contain some number of errors, also affectionately called “bugs.” The Great
Question of Programming is how we can reduce the number of bugs without slowing
the process of programming to a halt. To start with, we need to answer the following
question: just who is supposed to catch these bugs?