It’s 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, you’ll 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. You’ll not only find recipes for identifying errors during your program’s compilation, runtime, and testing phases, you’ll 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
A Bug-Hunting Strategy for C++
Chapter 1 Where Do C++ Bugs Come From?
Chapter 2 When to Catch a Bug
Why the Compiler Is Your Best Place to Catch Bugs
How to Catch Bugs in the Compiler
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
Chapter 5 Pointer Arithmetic
Chapter 6 Invalid Pointers, References, and Iterators
Chapter 7 Uninitialized Variables
Initialized Numbers (int, double, etc.)
Chapter 8 Memory Leaks
Reference Counting Pointers
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
Appendix Source Code for the scpp Library Used in This Book
Appendix Source Code for the files scpp_assert.hpp and scpp_assert.cpp
Appendix Source Code for the file scpp_vector.hpp
Appendix Source Code for the file scpp_array.hpp
Appendix Source Code for the file scpp_matrix.hpp
Appendix Source Code for the file scpp_types.hpp
Appendix Source Code for the file scpp_refcountptr.hpp
Appendix Source Code for the file scpp_scopedptr.hpp
Appendix Source Code for the file scpp_ptr.hpp
Appendix Source Code for the file scpp_date.hpp and scpp_date.cpp
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.
The animal on the cover of Safe C++ is is the merlin (Falcon columbarius). Formerly known as the pigeon hawk, this bird was embraced for falconry among royalty in medieval Europe, especially by women—Mary Queen of Scots and Catherine the Great, in particular. There are various subspecies of the merlin, and it could be argued that there are two distinct variants: the North American merlin and the European merlin (Falcon aesalon).This species is found at high latitudes all over North America in various habitats, including marshes, open woodland, and prairies. Merlins are migratory and will travel as far as South America and North Africa for winter months.Merlins are lean, yet robust, birds of prey. Females are usually larger than males, growing up to nearly a foot in length with a wingspan of at least 20 inches. Female merlins average about 8 ounces in weight, while males weigh nearly 6 ounces. Both sexes also slightly differ in appearance: female birds carry dark brown feathers, while males usually have blue-gray colored plumage. Both have long, banded tails.The merlin is an aggressive predator; as an aerial forager, it relies on agility and speed for its hunts. When in pursuit of its meal, the merlin will often fly very low to the ground and use their surroundings to take their prey by surprise. They are also apt to capture prey midair. Its diet consists of smaller birds, such as sandpipers and the meadow pipit, as well as other small mammals and large insects.Merlins are resourceful creatures; instead of building their own nests, they roost in old nests of other birds, like magpies or crows. They are also monogamous animals, and are known to demonstrate acrobatic displays of courtship. In fact, paired merlins will hunt cooperatively, with one bird flushing the prey toward its mate.The cover image is from Johnson's Natural History. The cover font is Adobe ITC Garamond. The text font is Linotype Birka; the heading font is Adobe Myriad Condensed; and the code font is LucasFont’s TheSansMonoCondensed.
This book is all about writing C++ code in a way that saves you from making common mistakes, but all of the recommendations are regarding common mistakes that I would have been aware of after maybe 2 years of coding in C++. The recommendations also have a tendency to hurt readability. For example, the book suggests that you replace all of the basic types with classes, simply for the sake of initializing your variables without forgetting, which I think is pretty ridiculous. It's way more effective to simply remember to initialize your variables. I felt many of the other recommendations were similarly ridiculous.
So maybe useful for projects where it's absolutely necessary, but not useful for the average programmer
Bottom Line No, I would not recommend this to a friend
Safe C++ is quite interesting book, however, after reading it I have mixed feelings. At some point it is targeted at begineers. If you don't know how to avoid memory related mistakes, how to recover from the run-time errors, or how to hunt bugs you don't know much about safe coding yet. In that case this book is definitelly for you. On the other hand, it requires quite good knowledge in C++ related area. I think, that knowing C++ well triggers some knowledge regarding topics covered in the book as well. Anyway, there are few nice topics there, and let me discuss them briefly.
Book itself is divided into three parts. First – describing what can be the source of issue, second – providing overview for some basic strategies that can save your time and effort, and third – telling you how to prepare the code for delivery. I think that last part is there just for the purpose of being there. It is short, condense and doesn't teach you how to make things right. First part tells some obvious things regarding issues you can encounter during development. If you are real begginer this will be a good source of the information for you. If you are experienced developer, feel free to skip it. Most interesting part of the book can be found in second part. Here you will find the essence of the book. You will go over the sections covering different types of issues and you will be told how to overcome them. Each section contains brief summary that makes it much easier to learn things.
One thing that stroke me during the lecture was the strong assumption of the author that you will write self estime code from the scratch. Which is not quite a use-case in real life. Of course, following the rules proposed by Vladimir will definitelly help you develop better and safer applications, but sometimes this is not possible to develop in isolation. You will depend on external libraries, you will have to access external data sources, you will deal with the inherited code nobody remembers already. In these cases simple rules are not enough. What I would like to find in this book are the tools and methods for tracking the issues, memory leaks, code analysis etc. Let me explain by few examples. For example, Vladimir suggests using dedicated scpp::vector class instead of std::vector. This sounds fine, but he makes very strong assumption that you will never cast to std::vector even though you inherit from it. This is very strong assumption taking into account that most of the people use std::vector and you will have to cast sooner or later. Another example is to use smart pointers to track the allocations. But what about things allocated inside some legacy code? How to track memory leaks there? The same reffers to the section covering the code being easier to debug. The code might be better for debugging but definitelly not easier to read for developer. I know that you always have to make a tradeoff, but still, I think there are better ways to make debugger friendly code comparing to #ifdef DEBUG based inner fields of classes.
Overlally, I think this book will be very interesting for beginners who have already learned C++ but they lack the real life experience. When it comes to experienced developers, I am pretty sure you know most of the solutions presented in the book already. Otherwise you wouldn't be able to survive in the industry.
I got this book via O'Reilly Blogger Review Program.
I didn't check the back cover when I started to read this 150 page short (previous book was Programming Perl with 1000+ pages) book. Almost immediately it started to feel like infomercial about a c++ library made by the author. Half way through I checked the back cover and found out that the book also shows how to use this library but the back cover also promised more wider look in how to make bug free code.
I found very little of anything else than his Safe C++ library. There was some mentioning about testing, but nothing about unit-tests. Memory errors were talked a lot but no mentioning about Valgrind (or anything else) for hunting them, just saying use the library. In the end there was a chapter "Making Your Code Debugger-Friendly" which was basically the only place where debuggers were mentioned. About one quarter of the book was source code of the library. There was also a nothing on sanitizing the user inputs which goes under safety.
If the title had been "Safe C++ library" or something similar I would have been a lot more lax in the review, but with current title I disliked the book.
I wouldn't recommend the book to anybody. If you are newbie one needs to learn the standard tools and not some obscure library, if you are seasoned the stuff in the book is old news for you.
I'd rate this 1/5, with more correct title 2.5/5.
Bottom Line No, I would not recommend this to a friend
I have been programming in Arduino a lot lately and keep running into a lot of problems as I compile the code. That's where Safe C++ comes in really handy. This is a good reference for anyone that's looking to become a better programmer by knowing what the basic pitfalls are. If you're learning to program, this probably isn't going to be for you but if you already know how to program and are learning a new language or brushing up on one, this works out well for C++. Ultimately for Safe C++, you really should know the language beforehand as it goes through some important things such as pointers. If you know what a pointer is an how to use it, then you should be able to use this book just fine.
Disclaimer: This book was received as part of the O'Reilly Blogger Review Program. All comments represent my thoughts and opinions.
Bottom Line Yes, I would recommend this to a friend