CONTENTS Introduction 1 Part I The Techniques 3 Chapter 1 Start with a Good Design 5 Technique 1: Define the Problem 5 Technique 2: Use the 1-3-5 Rule 6 Technique 3: Solution in Search of a Problem 6 Technique 4: Problem in Search of a Solution 7 Technique 5: In Search of a Solution and Problem 7 Technique 6: KISSKeep It Simple and Stupid 8 Technique 7: The Importance of Good Design 8 Technique 8: The Customer Is Always Right 9 Technique 9: Design Is an Iterative Process 9 Technique 10: Coding Is Also an Iterative Process 9 Technique 11: Work Smarter, Not Longer 10 Technique 12: Four Eyes Are Better than Two 10 Technique 13: Measure Twice, Cut Once 11 Technique 14: Know When to Say When 11 Technique 15: Pick the Right Algorithm and Data Structures for the Problem 11 Chapter 2 Darn Reasonable Practices 13 Technique 16: Be Careful When Cutting and Pasting Code 13 Technique 17: Initialize Local Variables 14 Technique 18: Validate Arguments 15 Technique 19: Check Return Codes 16 Technique 20: Check for 0 before Dividing 17 Technique 21: Check for 0 before Mod'ing 18 Technique 22: Write the Loop Increment/Decrement First 18 Technique 23: 0 == a Versus a == 0 19 < previous page page_v next page > < previous page page_vi next page > Page vi Technique 24: Use Header Sentinels 20 Technique 25: Intrinsics Use Byte Counts 21 Technique 26: memcpy Uses Byte Counts 22 Technique 27: Use Const Instead of Numeric Literals 22 Technique 28: Const Precomputes Sizes 23 Technique 29: Sometimes You Need to Use #define Instead of Const 24 Technique 30: Using an Assert Instead of, or in Addition to, Commenting Code 24 Technique 31: Handle the Exception Even If You Assert 25 Technique 32: Carefully Choose Signed or Unsigned Types 26 Technique 33: Be Careful When You Mix Signed and Unsigned Values 27 Technique 34: Use Parentheses If You Need Them 28 Technique 35: Use Parentheses If You Need Them 28 Technique 36: Always Treat Warnings as Errors 29 Technique 37: Always Use at Least Warning Level 3 30 Technique 38: Be Careful When Using in Strings 31 Technique 39: Be Careful When You Mix C and C++ 31 Technique 40: Determining If a Constant Is Already Defined 32 Technique 41: Know Exactly What Functions Do (At Least If You Use Them) 32 Technique 42: Make Sure Strings Are Null Terminated 34 Technique 43: Check the Actual Size 34 Technique 44: Use Inline Functions Instead of Macros 35 Chapter 3 Dealing with Compiler-Generated Code 37 Technique 45: Constructors and Destructors Are Automatically Created for You 37 Technique 46: Wrap New to Save Space 39 Technique 47: If You Overload New, Overload Delete 40 Technique 48: The + Operator Generates Considerable Overhead 41 Chapter 4 Pointers and Memory 43 Technique 49: Check for Successful Allocation 43 Technique 50: Free Once, Not Often 44 Technique 51: Make Sure You Can Allocate before You Replace 44 Technique 52: Be Prepared for Multiple Calls to Buffer Allocation Methods 45 Technique 53: Don't Return Pointers to Automatic Variables 46 < previous page page_vi next page > < previous page page_vii next page > Page vii Technique 54: Always Initialize to a Known State 47 Technique 55: Avoid Self-Referential Copies 48 Technique 56: Use Const * 49 Technique 57: Avoiding Errors When Using Const Const 50 Technique 58: Use Smart Pointers 51 Technique 59: Use Smart Pointers for Objects 52 Technique 60: Custom Memory Allocators and Smart Pointers 53 Technique 61: Don't Blow Your Buffers 53 Technique 62: Create a Memory Manager That Detects Leaks 54 Technique 63: Global Versus Class-Specific Memory Allocators 54 Chapter 5 Arrays 55 Technique 64: Use Delete [] with Arrays 55 Technique 65: Avoid Index Underflow 55 Technique 66: Avoid Index Overflow 56 Technique 67: Foo[K] Is the Same as Fodata:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABAQMAAAAl21bKAAAAA1BMVEXh5PJm+yKVAAAAAXRSTlMAQObYZgAAAApJREFUCNdjYAAAAAIAAeIhvDMAAAAASUVORK5CYII=" class="mceSmilieSprite mceSmilie12" alt="o_O" title="Er . what? o_O">perator[](K) 57 Technique 68: An Array Is a Pointer 58 Chapter 6 Classes 59 Technique 69: Initialize Member Variables 59 Technique 70: Use Initialization Lists 61 Technique 71: Don't Initialize Consts inside the Body of the Constructor 62 Technique 72: Const MemberVariables Allocate Memory 63 Technique 73: Nonstatic Consts Are Allocated in the Code Area 63 Technique 74: Wrap Member Variables to Protect from Outside Access 64 Technique 75: Keeping Your News Private 66 Technique 76: Using Runtime Type Information 67 Technique 77: If You Allocate Memory, Override the = Operator 68 Technique 78: Guard against Self-Referential Operators 70 Technique 79: Too Much Parameter Checking Is Still Too Much 71 Technique 80: Creating Const Operators 72 Technique 81: Private Derivation 74 Technique 82: Making Sure a Class or Structure Size Stays within Bounds 75 Technique 83: Use Inheritance Instead of Unions 76 Technique 84: Be Careful with Initialization When You Use Unions 78 Technique 85: Don't Cast to a Derived Class 80 Technique 86: Be Very Careful When Using Member Variables after Deleting This 81