Fluent C: Principles, Practices, and Patterns

This document was uploaded by one of our users. The uploader already confirmed that they had the permission to publish it. If you are author/publisher or own the copyright of this documents, please report to us by using this DMCA report form.

Simply click on the Download Book button.

Yes, Book downloads on Ebookily are 100% Free.

Sometimes the book is free on Amazon As well, so go ahead and hit "Search on Amazon"

Expert advice on C programming is hard to find. While much help is available for object-oriented programming languages, there's surprisingly little for the C language. With this hands-on guide, beginners and experienced C programmers alike will find guidance about design decisions, including how to apply them bit by bit to running code examples when building large-scale programs. Christopher Preschern, a leading member of the design patterns community, answers questions such as how to structure C programs, cope with error handling, or design flexible interfaces. Whether you're looking for one particular pattern or an overview of design options for a specific topic, this book shows you how to implement hands-on design knowledge specifically for the C programming language. You'll find design patterns for: • Error handling • Returning error information • Memory management • Returning data from C functions • Data lifetime and ownership • Flexible APIs • Flexible iterator interfaces • Organizing files in modular programs • Escaping #ifdef Hell

Author(s): Christopher Preschern
Edition: 1
Publisher: O'Reilly Media
Year: 2022

Language: English
Commentary: Publisher's PDF
Pages: 308
City: Sebastopol, CA
Tags: Logging; Design Patterns; Memory Management; Best Practices; Modules; C; Error Handling; APIs

Cover
Copyright
Table of Contents
Preface
Why I Wrote This Book
Patterns Basics
How to Read This Book
Overview of the Patterns
Conventions Used in This Book
Using Code Examples
O’Reilly Online Learning
How to Contact Us
Acknowledgments
Part I. C Patterns
Chapter 1. Error Handling
Running Example
Function Split
Context
Problem
Solution
Consequences
Known Uses
Applied to Running Example
Guard Clause
Context
Problem
Solution
Consequences
Known Uses
Applied to Running Example
Samurai Principle
Context
Problem
Solution
Consequences
Known Uses
Applied to Running Example
Goto Error Handling
Context
Problem
Solution
Consequences
Known Uses
Applied to Running Example
Cleanup Record
Context
Problem
Solution
Consequences
Known Uses
Applied to Running Example
Object-Based Error Handling
Context
Problem
Solution
Consequences
Known Uses
Applied to Running Example
Summary
Further Reading
Outlook
Chapter 2. Returning Error Information
Running Example
Return Status Codes
Context
Problem
Solution
Consequences
Known Uses
Applied to Running Example
Return Relevant Errors
Context
Problem
Solution
Consequences
Known Uses
Applied to Running Example
Special Return Values
Context
Problem
Solution
Consequences
Known Uses
Applied to Running Example
Log Errors
Context
Problem
Solution
Consequences
Known Uses
Applied to Running Example
Summary
Further Reading
Outlook
Chapter 3. Memory Management
Data Storage and Problems with Dynamic Memory
Running Example
Stack First
Context
Problem
Solution
Consequences
Known Uses
Applied to Running Example
Eternal Memory
Context
Problem
Solution
Consequences
Known Uses
Applied to Running Example
Lazy Cleanup
Context
Problem
Solution
Consequences
Known Uses
Applied to Running Example
Dedicated Ownership
Context
Problem
Solution
Consequences
Known Uses
Applied to Running Example
Allocation Wrapper
Context
Problem
Solution
Consequences
Known Uses
Applied to Running Example
Pointer Check
Context
Problem
Solution
Consequences
Known Uses
Applied to Running Example
Memory Pool
Context
Problem
Solution
Consequences
Known Uses
Applied to Running Example
Summary
Further Reading
Outlook
Chapter 4. Returning Data from C Functions
Running Example
Return Value
Context
Problem
Solution
Consequences
Known Uses
Applied to Running Example
Out-Parameters
Context
Problem
Solution
Consequences
Known Uses
Applied to Running Example
Aggregate Instance
Context
Problem
Solution
Consequences
Known Uses
Applied to Running Example
Immutable Instance
Context
Problem
Solution
Consequences
Known Uses
Applied to Running Example
Caller-Owned Buffer
Context
Problem
Solution
Consequences
Known Uses
Applied to Running Example
Callee Allocates
Context
Problem
Solution
Consequences
Known Uses
Applied to Running Example
Summary
Outlook
Chapter 5. Data Lifetime and Ownership
Stateless Software-Module
Context
Problem
Solution
Consequences
Known Uses
Applied to Running Example
Software-Module with Global State
Context
Problem
Solution
Consequences
Known Uses
Applied to Running Example
Caller-Owned Instance
Context
Problem
Solution
Consequences
Known Uses
Applied to Running Example
Shared Instance
Context
Problem
Solution
Consequences
Known Uses
Applied to Running Example
Summary
Further Reading
Outlook
Chapter 6. Flexible APIs
Header Files
Context
Problem
Solution
Consequences
Known Uses
Applied to Running Example
Handle
Context
Problem
Solution
Consequences
Known Uses
Applied to Running Example
Dynamic Interface
Context
Problem
Solution
Consequences
Known Uses
Applied to Running Example
Function Control
Context
Problem
Solution
Consequences
Known Uses
Applied to Running Example
Summary
Further Reading
Outlook
Chapter 7. Flexible Iterator Interfaces
Running Example
Index Access
Context
Problem
Solution
Consequences
Known Uses
Applied to Running Example
Cursor Iterator
Context
Problem
Solution
Consequences
Known Uses
Applied to Running Example
Callback Iterator
Context
Problem
Solution
Consequences
Known Uses
Applied to Running Example
Summary
Further Reading
Outlook
Chapter 8. Organizing Files in Modular Programs
Running Example
Include Guard
Context
Problem
Solution
Consequences
Known Uses
Applied to Running Example
Software-Module Directories
Context
Problem
Solution
Consequences
Known Uses
Applied to Running Example
Global Include Directory
Context
Problem
Solution
Consequences
Known Uses
Applied to Running Example
Self-Contained Component
Context
Problem
Solution
Consequences
Known Uses
Applied to Running Example
API Copy
Context
Problem
Solution
Consequences
Known Uses
Applied to Running Example
Summary
Outlook
Chapter 9. Escaping #ifdef Hell
Running Example
Avoid Variants
Context
Problem
Solution
Consequences
Known Uses
Applied to Running Example
Isolated Primitives
Context
Problem
Solution
Consequences
Known Uses
Applied to Running Example
Atomic Primitives
Context
Problem
Solution
Consequences
Known Uses
Applied to Running Example
Abstraction Layer
Context
Problem
Solution
Consequences
Known Uses
Applied to Running Example
Split Variant Implementations
Context
Problem
Solution
Consequences
Known Uses
Applied to Running Example
Summary
Further Reading
Outlook
Part II. Pattern Stories
Chapter 10. Implementing Logging Functionality
The Pattern Story
File Organization
Central Logging Function
Logging Source Filter
Conditional Logging
Multiple Logging Destinations
File Logging
Cross-Platform Files
Using the Logger
Summary
Chapter 11. Building a User Management System
The Pattern Story
Data Organization
File Organization
Authentication: Error Handling
Authentication: Error Logging
Adding Users: Error Handling
Iterating
Using the User Management System
Summary
Chapter 12. Conclusion
What You’ve Learned
Further Reading
Closing Remarks
Index
About the Author
Colophon