Synopses & Reviews
The C++ programming language allows programmer to make all kinds of mistakes such as access memory beyond the bounds of an allocated array, or read memory which was never initialized, or allocate memory and forget to de-allocate it, e.g. there are great many ways to “shoot yourself in a foot” while programming in C++, and the C++ language will not catch you by the hand and instead will proceed happily until it will either crash or produce an unreasonable result or do something which is in computer literature called by a vague but scary term “an unpredictable behavior”. So, in this sense, the C++ is unsafe.
This book contains discussion of some of the most typical mistakes made by us, programmers, in C++ code and also some recipes how to avoid each of these mistakes. The undeniable truth is that any program significantly more complex than “Hello, World” would contain some number of errors, also affectionately called “bugs”, which are put in there by the Programmer. The Great Question of Programming is how do we make the number of these bugs smaller (that is, without slowing the process of programming to a halt)?
Synopsis
Its easy to make lots of programming mistakes in C++—in fact, any program over a few hundred lines is likely to contain bugs. With this book, youll learn about many common coding errors that C++ programmers produce, along with rules and strategies you can use to avoid them.
Author Vladimir Kushnir shows you how to use his Safe C++ library, based in part on programming practices developed by the C++ community. Youll not only find recipes for identifying errors during your programs compilation, runtime, and testing phases, youll learn a comprehensive approach for making your C++ code safe and bug-free.
- Get recipes for handling ten different error types, including memory leaks and uninitialized variables
- Discover problems C++ inherited from C, like pointer arithmetic
- Insert temporary and permanent sanity checks to catch errors at runtime
- Apply bug prevention techniques, such as using separate classes for each data type
- Pursue a testing strategy to hunt and fix one bug at a time—before your code goes into production
About the Author
Vladimir Kushnir obtained his Ph. D. in physics at the Institute for Solid State Physics, Academy of Sciences of the USSR. Since that time, Vladimir worked as an experimental physicist, using FORTRAN, C and then C++, while working at Northwestern University and later at the Argonne National Laboratory. He then went to work with Wall Street firms, focusing mostly on calculations called “financial analytics”, and having special interest in taking a calculation and making it run faster, sometimes by an order of magnitude. He lives with his wife Daria in Connecticut and when not programming in C++, enjoys Jazz music and underwater photography in his spare time.
Table of Contents
Preface; Audience; How This Book Is Organized; Conventions Used in This Book; Naming Conventions; Using Code Examples; Safari® Books Online; How to Contact Us; Acknowledgments; A Bug-Hunting Strategy for C++; Chapter 1: Where Do C++ Bugs Come From?; Chapter 2: When to Catch a Bug; 2.1 Why the Compiler Is Your Best Place to Catch Bugs; 2.2 How to Catch Bugs in the Compiler; 2.3 The Proper Way to Handle Types; Chapter 3: What to Do When We Encounter an Error at Runtime; Bug Hunting: One Bug at a Time; Chapter 4: Index Out of Bounds; 4.1 Dynamic Arrays; 4.2 Static Arrays; 4.3 Multidimensional Arrays; Chapter 5: Pointer Arithmetic; Chapter 6: Invalid Pointers, References, and Iterators; Chapter 7: Uninitialized Variables; 7.1 Initialized Numbers (int, double, etc.); 7.2 Uninitialized Boolean; Chapter 8: Memory Leaks; 8.1 Reference Counting Pointers; 8.2 Scoped Pointers; 8.3 Enforcing Ownership with Smart Pointers; Chapter 9: Dereferencing NULL Pointers; Chapter 10: Copy Constructors and Assignment Operators; Chapter 11: Avoid Writing Code in Destructors; Chapter 12: How to Write Consistent Comparison Operators; Chapter 13: Errors When Using Standard C Libraries; The Joy of Bug Hunting: From Testing to Debugging to Production; Chapter 14: General Testing Principles; Chapter 15: Debug-On-Error Strategy; Chapter 16: Making Your Code Debugger-Friendly; Chapter 17: Conclusion; Source Code for the scpp Library Used in This Book; Source Code for the files scpp_assert.hpp and scpp_assert.cpp; Source Code for the file scpp_vector.hpp; Source Code for the file scpp_array.hpp; Source Code for the file scpp_matrix.hpp; Source Code for the file scpp_types.hpp; Source Code for the file scpp_refcountptr.hpp; Source Code for the file scpp_scopedptr.hpp; Source Code for the file scpp_ptr.hpp; Source Code for the file scpp_date.hpp and scpp_date.cpp; Colophon;