You are previewing Expert C Programming: Deep C Secrets.
O'Reilly logo
Expert C Programming: Deep C Secrets

Book Description

This book is for the knowledgeable C programmer, this is a second book that gives the C programmers advanced tips and tricks. This book will help the C programmer reach new heights as a professional. Organized to make it easy for the reader to scan to sections that are relevant to their immediate needs.

Table of Contents

  1. Copyright
    1. Dedication
  2. Preface
  3. Acknowledgments
  4. Introduction
    1. The $20 Million Bug
    2. Convention
    3. Some Light Relief—Tuning File Systems
  5. 1. C Through the Mists of Time
    1. The Prehistory of C
    2. Early Experiences with C
    3. The Standard I/O Library and C Preprocessor
    4. K&R C
    5. The Present Day: ANSI C
    6. It’s Nice, but Is It Standard?
      1. Unportable Code:
      2. Bad Code:
      3. Portable Code:
    7. Translation Limits
    8. The Structure of the ANSI C Standard
    9. Reading the ANSI C Standard for Fun, Pleasure, and Profit
    10. How Quiet is a “Quiet Change”?
    11. Some Light Relief—The Implementation-Defined Effects of Pragmas . . .
  6. 2. It’s Not a Bug, It’s a Language Feature
    1. Why Language Features Matter—The Way the Fortran Bug Really Happened!
    2. Sins of Commission
      1. Switches Let You Down with Fall Through
      2. Available Hardware Is a Crayon?
      3. Too Much Default Visibility
    3. Sins of Mission
      1. Overloading the Camel’s Back
      2. “Some of the Operators Have the Wrong Precedence”
      3. The Early Bug gets() the Internet Worm
    4. Sins of Omission
      1. Mail Won’t Go to Users with an “f” in Their User names
      2. Space—The Final Frontier
      3. A Digression into C++ Comments
      4. The Compiler Date Is Corrupted
      5. Lint Should Never Have Been Separated Out
    5. Some Light Relief—Some Features Really Are Bugs!
      1. References
  7. 3. Unscrambling Declarations in C
    1. Syntax Only a Compiler Could Love
    2. How a Declaration Is Formed
      1. A Word About structs
      2. A Word About unions
      3. A Word About enums
    3. The Precedence Rule
    4. Unscrambling C Declarations by Diagram
    5. typedef Can Be Your Friend
    6. Difference Between typedef int x[10] and #define x int[10]
    7. What typedef struct foo { ... foo; } foo; Means
    8. The Piece of Code that Understandeth All Parsing
      1. Further Reading
    9. Some Light Relief—Software to Bite the Wax Tadpole…
  8. 4. The Shocking Truth: C Arrays and Pointers Are NOT the Same!
    1. Arrays Are NOT Pointers!
    2. Why Doesn’t My Code Work?
    3. What’s a Declaration? What’s a Definition?
    4. How Arrays and Pointers Are Accessed
      1. What Happens When You “Define as Pointer/Reference as Array”
    5. Match Your Declarations to the Definition
    6. Other Differences Between Arrays and Pointers
    7. Some Light Relief—Fun with Palindromes!
  9. 5. Thinking of Linking
    1. Libraries, Linking, and Loading
      1. Where the Linker Is in the Phases of Compilation
    2. The Benefits of Dynamic Linking
    3. Five Special Secrets of Linking with Libraries
    4. Watch Out for Interpositioning
    5. Generating Linker Report Files
    6. Some Light Relief—Look Who’s Talking: Challenging the Turing Test
      1. Eliza
      2. Eliza Meets the VP
      3. Doctor, Meet Doctor
      4. The Prize in Boston
      5. Conclusions
      6. Postscript
      7. Further Reading
  10. 6. Poetry in Motion: Runtime Data Structures
    1. a.out and a.out Folklore
    2. Segments
    3. What the OS Does with Your a.out
    4. What the C Runtime Does with Your a.out
      1. The Stack Segment
    5. What Happens When a Function Gets Called: The Procedure Activation Record
    6. The auto and static keywords
      1. A Stack Frame Might Not Be on the Stack
    7. Threads of Control
    8. setjmp and longjmp
    9. The Stack Segment Under UNIX
    10. The Stack Segment Under MS-DOS
    11. Helpful C Tools
    12. Some Light Relief—Programming Puzzles at CMU
    13. For Advanced Students Only
  11. 7. Thanks for the Memory
    1. The Intel 80x86 Family
    2. The Intel 80x86 Memory Model and How It Got That Way
    3. Virtual Memory
    4. Cache Memory
    5. The Data Segment and Heap
    6. Memory Leaks
      1. How to Check for a Memory Leak
    7. Bus Error, Take the Train
      1. Bus Error
      2. Segmentation Fault
    8. Some Light Relief—The Thing King and the Paging Game
      1. The Paging Game
        1. Rules
        2. Notes
        3. Rules
        4. Notes
  12. 8. Why Programmers Can’t Tell Halloween from Christmas Day
    1. The Potrzebie System of Weights and Measures
    2. Making a Glyph from Bit Patterns
    3. Types Changed While You Wait
    4. Prototype Painfulness
      1. Where Prototypes Break Down
    5. Getting a Char Without a Carriage Return
    6. Implementing a Finite State Machine in C
    7. Software Is Harder than Hardware!
    8. How and Why to Cast
    9. Some Light Relief—The Inter national Obfuscated C Code Competition
  13. 9. More about Arrays
    1. When an Array Is a Pointer
    2. Why the Confusion?
      1. Rule 1. An “Array Name in an Expression” Is a Pointer
      2. Rule 2. C Treats Array Subscripts as Pointer Offsets
      3. Rule 3. An “Array Name as a Function Parameter” Is a Pointer
    3. Why C Treats Array Parameters as Pointers
      1. How an Array Parameter Is Referenced
    4. Indexing a Slice
    5. Arrays and Pointers Interchangeability Summary
    6. C Has Multidimensional Arrays…
    7. …But Every Other Language Calls Them “Arrays of Arrays”
    8. How Multidimensional Arrays Break into Components
    9. How Arrays Are Laid Out in Memory
    10. How to Initialize Arrays
    11. Some Light Relief—Hardware/Software Trade-Offs
  14. 10. More About Pointers
    1. The Layout of Multidimensional Arrays
    2. An Array of Pointers Is an “Iliffe Vector”
    3. Using Pointers for Ragged Arrays
      1. For Advanced Students Only
    4. Passing a One-Dimensional Array to a Function
    5. Using Pointers to Pass a Multidimensional Array to a Function
      1. Attempt 1
      2. Attempt 2
      3. Attempt 3
      4. Attempt 4
    6. Using Pointers to Return an Array from a Function
    7. Using Pointers to Create and Use Dynamic Arrays
    8. Some Light Relief—The Limitations of Program Proofs
      1. Further Reading
  15. Further Reading
  16. Secrets of Programmer Job Interviews
    1. Silicon Valley Programmer Interviews
    2. How Can You Detect a Cycle in a Linked List?
    3. What Are the Different C Increment Statements For?
      1. Moral: Don’t try to do too many things in one statement.
    4. How Is a Library Call Different from a System Call?
    5. How Is a File Descriptor Different from a File Pointer?
    6. Write Some Code to Determine if a Variable Is Signed or Not
    7. What Is the Time Complexity of Printing the Values in a Binary Tree?
    8. Give Me a String at Random from This File
    9. Some Light Relief—How to Measure a Building with a Barometer
    10. Further Reading
      1. How to make Oobleck