Memory Thinking for C & C++ Linux Diagnostics
Memory Thinking for C & C++ Linux Diagnostics
Slides with Descriptions and Source Code Illustrations, Second Edition
About the Book
Solid C and C++ knowledge is a must to fully understand Linux diagnostic artifacts such as core memory dumps and do diagnostic, forensic, and root cause analysis beyond listing backtraces. This full-color reference book is a part of the Accelerated C & C++ for Linux Diagnostics training course organized by Software Diagnostics Services. The text contains slides, brief notes highlighting particular points, and source code illustrations. In addition to new topics, the second edition adds 45 projects with more than 5,500 lines of code. The book's detailed Table of Contents makes the usual Index redundant. We hope this reference is helpful for the following audiences:
- C and C++ developers who want to deepen their knowledge
- Software engineers developing and maintaining products on Linux platforms
- Technical support, escalation, DevSecOps, cloud and site reliability engineers dealing with complex software issues
- Quality assurance engineers who test software on Linux platforms
- Security and vulnerability researchers, reverse engineers, malware and memory forensics analysts
Bundles that include this book
Table of Contents
Preface 11
About the Author 12
Introduction 13
Original Training Course Name 13
Prerequisites 13
Training Goals 14
Training Principles 14
Schedule 15
Training Idea 15
Version 2.0 Idea 16
General C & C++ Aspects 16
What We Do Not Cover 18
Linux C & C++ Aspects 18
Why C & C++? 19
Which C & C++? 20
My History of C & C++ 20
C and C++ Mastery Process 21
Thought Process 22
Philosophy of Pointers 23
Pointer 23
Pointer Dereference 24
One to Many 24
Many to One 25
Many to One Dereference 25
Invalid Pointer 26
Invalid Pointer Dereference 26
Wild (Dangling) Pointer 27
Pointer to Pointer 27
Pointer to Pointer Dereference 28
Naming Pointers and Entities 28
Names as Pointer Content 29
Pointers as Entities 29
Pointer Code Examples 30
Warning 31
Pointer 31
* Placement Style 33
Pointer Dereference 33
One to Many 35
Memory Leak 36
Many to One 38
Many to One Dereference 40
Invalid Pointer 41
Invalid Pointer Dereference 43
Wild (Dangling) Pointer 44
Pointer to Pointer 46
Pointer to Pointer Dereference 48
Undefined Behavior 49
Appendix 50
Memory and Pointers 51
Mental Exercise 52
Debugger Memory Layout 52
Memory Dereference Layout 53
Names as Addresses 53
Addresses and Entities 54
Addresses and Structures 54
Pointers to Structures 55
Arrays 55
Arrays and Pointers to Arrays 56
Strings and Pointers to Strings 57
Appendix 59
Basic Types 60
ASCII Characters and Pointers 61
Bytes and Pointers 62
Wide Characters and Pointers 63
Integers 64
Little-Endian System 65
Short Integers 66
Long and Long Long Integers 67
Signed and Unsigned Integers 68
Fixed Size Integers 69
Booleans 70
Bytes 71
Alignment (C11) 72
Alignment (C++11) 73
Size 74
LP64 75
Nothing and Anything 76
Automatic Type Inference 77
Appendix 78
Entity Conversion 80
Pointer Conversion (C-Style) 81
Pointer Conversion (C++) 82
Numeric Promotion/Conversion 83
Numeric Conversion 84
Incompatible Types 85
Forcing 86
Uniting 87
Appendix 89
Structures, Classes, and Objects 90
Structures 91
Access Level 92
Reading/Writing Private Fields 93
Classes and Objects 94
Structures and Classes 95
Pointer to Structure 96
Pointer to Structure Dereference 97
One to Many 98
Memory Leak 100
Many Pointers to One Structure 102
Many to One Dereference 103
Invalid Pointer to Structure 105
Invalid Pointer Dereference 107
Wild (Dangling) Pointer 108
Pointer to Pointer to Structure 111
Pointer to Pointer Dereference 112
Appendix 114
Memory and Structures 116
Addresses and Structures 117
Structure Field Access 118
Pointers to Structures 120
Pointers to Structure Fields 121
Structure Inheritance 124
Structure Slicing 125
Inheritance Access Level 127
Structures and Classes II 128
Reading/Writing Private Base 129
Internal Structure Alignment 130
Static Structure Fields 131
Appendix 132
Uniform Initialization 134
Old Initialization Ways 135
New Way {} 136
Uniform Structure Initialization 137
Static Field Initialization 139
Macros, Types, and Synonyms 140
Macros 141
Old Way 142
New Way 143
Enumerations 144
Old Way 145
New Way 146
Appendix 147
Memory Storage 148
Overview 149
Memory Regions 149
Dynamic Virtual Memory 150
Static Memory 150
Stack Memory 151
Thread Stack Frames 151
Local Variable Value Lifecycle 152
Scope 154
Stack Allocation Pitfalls 155
Explicit Local Allocation 156
Heap Memory 157
Dynamic Allocation (C-style) 158
Dynamic Allocation (C++) 158
Memory Expressions 159
Memory Operators 159
Local Pointers (Manual) 162
In-place Allocation 163
Useful GDB Commands 164
Appendix 165
Source Code Organisation 166
Logical Layer (Translation Units) 167
Physical Layer (Source Files) 167
Inter-TU Sharing 168
Classic Static TU Isolation 168
Namespace TU Isolation 169
Declaration and Definition 169
TU Definition Conflicts 170
Fine-grained TU Scope Isolation 171
Conceptual Layer (Design) 172
Incomplete Types 172
References 175
Type& vs. Type* 176
Values 177
Value Categories 178
Constant Values 179
Constant Expressions 181
Functions 182
Macro Functions 183
constexpr Functions 184
Pointers to Functions 185
Function Pointer Types 186
Reading Declarations 187
Structure Function Fields 188
Structure Methods 189
Structure Methods (Inlined) 190
Structure Methods (Inheritance) 191
Structure Virtual Methods 192
Structure Pure Virtual Methods 195
Structure as Interface 197
Function Structure 199
Structure Constructors 200
Structure Converting Constructors 201
Structure Delegating Constructors 202
Structure Member Initialization 204
Structure Copy Constructor 205
Copy vs. Move Semantics 206
Structure Move Constructors 207
Structure Copy Assignment 208
Structure Move Assignment 210
Structure Destructor 212
Structure Destructor Hierarchy 213
Structure Virtual Destructor 214
Structure Member Destruction 216
Destructor as a Method 217
Structure Default Operations 218
Structure Deleted Operations 219
Conversion Operators 221
Parameters by Value 222
Parameters by Pointer/Reference 224
Parameters by Ptr/Ref to Const 226
Parameters by Ref to Rvalue 228
Possible Mistake 229
Function Overloading 229
Default Arguments 230
Variadic Functions 231
Immutable Objects 232
Static Structure Functions 233
Lambdas 234
x64 CPU Registers 234
x64 Instructions and Registers 235
x64 Memory and Stack Addressing 235
x64 Memory Load Instructions 236
x64 Memory Store Instructions 236
x64 Flow Instructions 237
x64 Function Parameters 237
x64 Struct Function Parameters 238
A64 CPU Registers 238
A64 Instructions and Registers 239
A64 Memory and Stack Addressing 239
A64 Memory Load Instructions 240
A64 Memory Store Instructions 240
A64 Flow Instructions 241
A64 Function Parameters 241
A64 Struct Function Parameters 242
this 243
Function Objects vs. Lambdas 244
A64 Lambda Example 245
Captures and Closures 245
A64 Captures Examples 246
Lambdas as Parameters 247
A64 Lambda Parameter Example 248
Lambda Parameter Optimization 248
A64 Optimization Example 249
Lambdas as Unnamed Functions 250
std::function Lambda Parameters 251
auto Lambda Parameters 252
Lambdas as Return Values 253
Appendix 254
Virtual Function Call 257
VTBL Memory Layout 258
VPTR and Struct Memory Layout 258
Templates: A Planck-length Introduction 260
Why Templates? 261
Reusability 261
Types of Templates 262
Types of Template Parameters 263
Type Safety 264
Flexibility 265
Metafunctions 266
Variadic Templates 267
Iterators as Pointers 268
Containers 269
Iterators 269
Constant Iterators 270
Pointers as Iterators 271
Algorithms 272
Memory Ownership 274
Pointers as Owners 275
Problems with Pointer Owners 275
Smart Pointers 276
Basic Design 277
Unique Pointers 277
Descriptors as Unique Pointers 278
Shared Pointers 279
RAII 281
RAII Definition 282
RAII Advantages 282
File Descriptor RAII 283
Threads and Synchronization 285
Threads in C/C++ 286
Threads in C++ Proper 287
Synchronization Problems 288
Synchronization Solution 289
Memory-safe C++ Development 290
URSS Principle 290
Resources 291
C and C++ 291
Training (Linux C and C++) 292
Other books by this author
The Leanpub 60 Day 100% Happiness Guarantee
Within 60 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.
Now, this is technically risky for us, since you'll have the book or course files either way. But we're so confident in our products and services, and in our authors and readers, that we're happy to offer a full money back guarantee for everything we sell.
You can only find out how good something is by trying it, and because of our 100% money back guarantee there's literally no risk to do so!
So, there's no reason not to click the Add to Cart button, is there?
See full terms...
Earn $8 on a $10 Purchase, and $16 on a $20 Purchase
We pay 80% royalties on purchases of $7.99 or more, and 80% royalties minus a 50 cent flat fee on purchases between $0.99 and $7.98. You earn $8 on a $10 sale, and $16 on a $20 sale. So, if we sell 5000 non-refunded copies of your book for $20, you'll earn $80,000.
(Yes, some authors have already earned much more than that on Leanpub.)
In fact, authors have earnedover $14 millionwriting, publishing and selling on Leanpub.
Learn more about writing on Leanpub
Free Updates. DRM Free.
If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).
Most Leanpub books are available in PDF (for computers) and EPUB (for phones, tablets and Kindle). The formats that a book includes are shown at the top right corner of this page.
Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.
Learn more about Leanpub's ebook formats and where to read them