ebook img

Modern C Up and Running: A Programmer's Guide to Finding Fluency and Bypassing the Quirks PDF

371 Pages·2022·2.773 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Modern C Up and Running: A Programmer's Guide to Finding Fluency and Bypassing the Quirks

Modern C Up and Running A Programmer’s Guide to Finding Fluency and Bypassing the Quirks Martin Kalin Modern C Up and Running: A Programmer’s Guide to Finding Fluency and Bypassing the Quirks Martin Kalin Chicago, IL, USA ISBN-13 (pbk): 978-1-4842-8675-3 ISBN-13 (electronic): 978-1-4842-8676-0 https://doi.org/10.1007/978-1-4842-8676-0 Copyright © 2022 by Martin Kalin This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission or information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed. Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights. While the advice and information in this book are believed to be true and accurate at the date of publication, neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or omissions that may be made. The publisher makes no warranty, express or implied, with respect to the material contained herein. Managing Director, Apress Media LLC: Welmoed Spahr Acquisitions Editor: Steve Anglin Development Editor: James Markham Coordinating Editor: Jill Balzano Cover image designed by Freepik (www.freepik.com) Distributed to the book trade worldwide by Springer Science+Business Media LLC, 1 New York Plaza, Suite 4600, New York, NY 10004. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail [email protected], or visit www.springeronline.com. Apress Media, LLC is a California LLC and the sole member (owner) is Springer Science + Business Media Finance Inc (SSBM Finance Inc). SSBM Finance Inc is a Delaware corporation. For information on translations, please e-mail [email protected]; for reprint, paperback, or audio rights, please e-mail [email protected]. Apress titles may be purchased in bulk for academic, corporate, or promotional use. eBook versions and licenses are also available for most titles. For more information, reference our Print and eBook Bulk Sales web page at http://www.apress.com/bulk-sales. Any source code or other supplementary material referenced by the author in this book is available to readers on GitHub. Printed on acid-free paper To Janet, yet again. Table of Contents About the Author ���������������������������������������������������������������������������������xi About the Technical Reviewer �����������������������������������������������������������xiii Acknowledgments ������������������������������������������������������������������������������xv Preface ���������������������������������������������������������������������������������������������xvii Chapter 1: Program Structure ���������������������������������������������������������� 1 1.1. Overview .................................................................................................... 1 1.2. The Function .............................................................................................. 2 1.3. The Function main ..................................................................................... 5 1.4. C Functions and Assembly Callable Blocks ................................................ 8 1.4.1. A Simpler Program in Assembly Code ............................................... 14 1.5. Passing Command-Line Arguments to main .............................................. 16 1.6. Control Structures ...................................................................................... 19 1.7. Normal Flow of Control in Function Calls ................................................... 26 1.8. Functions with a Variable Number of Arguments ....................................... 28 1.9. What’s Next? .............................................................................................. 32 Chapter 2: Basic Data Types ������������������������������������������������������������� 33 2.1. Overview .................................................................................................... 33 2.2. Integer Types .............................................................................................. 35 2.2.1. A Caution on the 2’s Complement Representation ............................ 40 2.2.2. Integer Overflow ................................................................................ 41 v Table of ConTenTs 2.3. Floating-Point Types ................................................................................... 43 2.3.1. Floating-Point Challenges ................................................................. 44 2.3.2. IEEE 754 Floating-Point Types ........................................................... 49 2.4. Arithmetic, Bitwise, and Boolean Operators ............................................... 54 2.4.1. Arithmetic Operators ......................................................................... 56 2.4.2. Boolean Operators ............................................................................. 58 2.4.3. Bitwise Operators .............................................................................. 60 2.5. What’s Next? .............................................................................................. 64 Chapter 3: Aggregates and Pointers ������������������������������������������������� 67 3.1. Overview .................................................................................................... 67 3.2. Arrays ......................................................................................................... 68 3.3. Arrays and Pointer Arithmetic .................................................................... 69 3.4. More on the Address and Dereference Operators ...................................... 72 3.5. Multidimensional Arrays ............................................................................ 75 3.6. Using Pointers for Return Values ............................................................... 80 3.7. The void* Data Type and NULL ................................................................... 84 3.7.1. The void* Data Type and Higher-Order Callback Functions ............... 87 3.8. Structures .................................................................................................. 96 3.8.1. Sorting Pointers to Structures ........................................................... 99 3.8.2. Unions ................................................................................................103 3.9. String Conversions with Pointers to Pointers .............................................104 3.10. Heap Storage and Pointers ......................................................................109 3.11. The Challenge of Freeing Heap Storage ...................................................120 3.12. Nested Heap Storage ...............................................................................125 3.12.1. Memory Leakage and Heap Fragmentation ....................................131 3.12.2. Tools to Diagnose Memory Leakage ................................................132 3.13. What’s Next? ............................................................................................134 vi Table of ConTenTs Chapter 4: Storage Classes ���������������������������������������������������������������135 4.1. Overview ....................................................................................................135 4.2. Storage Class Basics .................................................................................135 4.3. The auto and register Storage Classes ......................................................138 4.4. The static Storage Class ............................................................................140 4.5. The extern Storage Class ...........................................................................142 4.6. The volatile Type Qualifier ..........................................................................147 4.7. What’s Next? ..............................................................................................150 Chapter 5: Input and Output �������������������������������������������������������������151 5.1. Overview ....................................................................................................151 5.2. System-Level I/O ........................................................................................152 5.2.1. Low-Level Opening and Closing ........................................................157 5.3. Redirecting the Standard Input, Standard Output, and Standard Error ......164 5.4. Nonsequential I/O .......................................................................................166 5.5. High-Level I/O ............................................................................................169 5.6. Unbuffered and Buffered I/O ......................................................................175 5.7. Nonblocking I/O ..........................................................................................178 5.7.1. A Named Pipe for Nonblocking I/O ....................................................180 5.8. What’s Next? ..............................................................................................188 Chapter 6: Networking ����������������������������������������������������������������������189 6.1. Overview ....................................................................................................189 6.2. A Web Client ...............................................................................................190 6.2.1. Utility Functions for the Web Client ...................................................196 6.3. An Event-Driven Web Server ......................................................................199 6.3.1. The webserver Program ....................................................................203 6.3.2. Utility Functions for the Web Server ..................................................204 6.3.3. Testing the Web Server with curl .......................................................212 vii Table of ConTenTs 6.4. Secure Sockets with OpenSSL ...................................................................214 6.5. What’s Next? ..............................................................................................229 Chapter 7: Concurrency and Parallelism ������������������������������������������231 7.1. Overview ....................................................................................................231 7.2. Multiprocessing Through Process Forking .................................................233 7.2.1. Safeguarding Against Zombie Processes ..........................................240 7.3. The exec Family of Functions .....................................................................241 7.3.1. Process Id and Exit Status .................................................................244 7.4. Interprocess Communication Through Shared Memory .............................247 7.5. Interprocess Communication Through File Locking ...................................256 7.6. Interprocess Communication Through Message Queues ...........................264 7.7. Multithreading ............................................................................................269 7.7.1. A Thread-Based Race Condition ........................................................273 7.7.2. The Miser/Spendthrift Race Condition ...............................................274 7.8. Deadlock in Multithreading ........................................................................281 7.9. SIMD Parallelism ........................................................................................285 7.10. What’s Next? ............................................................................................289 Chapter 8: Miscellaneous Topics ������������������������������������������������������291 8.1. Overview ....................................................................................................291 8.2. Regular Expressions ..................................................................................292 8.3. Assertions ..................................................................................................300 8.4. Locales and i18n ........................................................................................304 8.5. C and WebAssembly ...................................................................................313 8.5.1. A C into WebAssembly Example ........................................................315 8.5.2. The Emscripten Toolchain ..................................................................316 8.5.3. WebAssembly and Code Reuse .........................................................322 viii Table of ConTenTs 8.6. Signals .......................................................................................................323 8.7. Software Libraries ......................................................................................328 8.7.1. The Library Functions ........................................................................330 8.7.2. Library Source Code and Header File ................................................331 8.7.3. Steps for Building the Libraries .........................................................334 8.7.4. A Sample C Client ..............................................................................336 8.7.5. A Sample Python Client .....................................................................341 8.8. What’s Next? ..............................................................................................342 Index �������������������������������������������������������������������������������������������������345 ix About the Author Martin Kalin has a Ph.D. from Northwestern University and is a professor in the College of Computing and Digital Media at DePaul University. He has cowritten a series of books on C and C++ and written a book on Java web services. He enjoys commercial programming and has codeveloped, in C, large distributed systems in process scheduling and product configuration. He can be reached at http://condor.depaul.edu/mkalin. xi

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.