Understanding and Using C Pointers

Book description

Improve your programming through a solid understanding of C pointers and memory management. With this practical book, you’ll learn how pointers provide the mechanism to dynamically manipulate memory, enhance support for data structures, and enable access to hardware. Author Richard Reese shows you how to use pointers with arrays, strings, structures, and functions, using memory models throughout the book.

Difficult to master, pointers provide C with much flexibility and power—yet few resources are dedicated to this data type. This comprehensive book has the information you need, whether you’re a beginner or an experienced C or C++ programmer or developer.

  • Get an introduction to pointers, including the declaration of different pointer types
  • Learn about dynamic memory allocation, de-allocation, and alternative memory management techniques
  • Use techniques for passing or returning data to and from functions
  • Understand the fundamental aspects of arrays as they relate to pointers
  • Explore the basics of strings and how pointers are used to support them
  • Examine why pointers can be the source of security problems, such as buffer overflow
  • Learn several pointer techniques, such as the use of opaque pointers, bounded pointers and, the restrict keyword

Publisher resources

View/Submit Errata

Table of contents

  1. Preface
    1. Why This Book Is Different
    2. The Approach
    3. Audience
    4. Organization
    5. Summary
    6. Conventions Used in This Book
    7. Using Code Examples
    8. Safari® Books Online
    9. How to Contact Us
  2. 1. Introduction
    1. Pointers and Memory
      1. Why You Should Become Proficient with Pointers
      2. Declaring Pointers
      3. How to Read a Declaration
      4. Address of Operator
      5. Displaying Pointer Values
        1. Virtual memory and pointers
      6. Dereferencing a Pointer Using the Indirection Operator
      7. Pointers to Functions
      8. The Concept of Null
        1. To NULL or not to NULL
        2. Pointer to void
        3. Global and static pointers
    2. Pointer Size and Types
      1. Memory Models
      2. Predefined Pointer-Related Types
        1. Understanding size_t
        2. Using the sizeof operator with pointers
        3. Using intptr_t and uintptr_t
    3. Pointer Operators
      1. Pointer Arithmetic
        1. Adding an integer to a pointer
        2. Pointers to void and addition
        3. Subtracting an integer from a pointer
        4. Subtracting two pointers
      2. Comparing Pointers
    4. Common Uses of Pointers
      1. Multiple Levels of Indirection
      2. Constants and Pointers
        1. Pointers to a constant
        2. Constant pointers to nonconstants
        3. Constant pointers to constants
        4. Pointer to (constant pointer to constant)
    5. Summary
  3. 2. Dynamic Memory Management in C
    1. Dynamic Memory Allocation
      1. Memory Leaks
        1. Losing the address
        2. Hidden memory leaks
    2. Dynamic Memory Allocation Functions
      1. Using the malloc Function
        1. To cast or not to cast
        2. Failing to allocate memory
        3. Not using the right size for the malloc function
        4. Determining the amount of memory allocated
        5. Using malloc with static and global pointers
      2. Using the calloc Function
      3. Using the realloc Function
      4. The alloca Function and Variable Length Arrays
    3. Deallocating Memory Using the free Function
      1. Assigning NULL to a Freed Pointer
      2. Double Free
      3. The Heap and System Memory
      4. Freeing Memory upon Program Termination
    4. Dangling Pointers
      1. Dangling Pointer Examples
      2. Dealing with Dangling Pointers
      3. Debug Version Support for Detecting Memory Leaks
    5. Dynamic Memory Allocation Technologies
      1. Garbage Collection in C
      2. Resource Acquisition Is Initialization
      3. Using Exception Handlers
    6. Summary
  4. 3. Pointers and Functions
    1. Program Stack and Heap
      1. Program Stack
      2. Organization of a Stack Frame
    2. Passing and Returning by Pointer
      1. Passing Data Using a Pointer
      2. Passing Data by Value
      3. Passing a Pointer to a Constant
      4. Returning a Pointer
      5. Pointers to Local Data
      6. Passing Null Pointers
      7. Passing a Pointer to a Pointer
        1. Writing your own free function
    3. Function Pointers
      1. Declaring Function Pointers
      2. Using a Function Pointer
      3. Passing Function Pointers
      4. Returning Function Pointers
      5. Using an Array of Function Pointers
      6. Comparing Function Pointers
      7. Casting Function Pointers
    4. Summary
  5. 4. Pointers and Arrays
    1. Quick Review of Arrays
      1. One-Dimensional Arrays
      2. Two-Dimensional Arrays
      3. Multidimensional Arrays
    2. Pointer Notation and Arrays
      1. Differences Between Arrays and Pointers
    3. Using malloc to Create a One-Dimensional Array
    4. Using the realloc Function to Resize an Array
    5. Passing a One-Dimensional Array
      1. Using Array Notation
      2. Using Pointer Notation
    6. Using a One-Dimensional Array of Pointers
    7. Pointers and Multidimensional Arrays
    8. Passing a Multidimensional Array
    9. Dynamically Allocating a Two-Dimensional Array
      1. Allocating Potentially Noncontiguous Memory
      2. Allocating Contiguous Memory
    10. Jagged Arrays and Pointers
    11. Summary
  6. 5. Pointers and Strings
    1. String Fundamentals
      1. String Declaration
      2. The String Literal Pool
        1. When a string literal is not a constant
      3. String Initialization
        1. Initializing an array of char
        2. Initializing a pointer to a char
        3. Initializing a string from standard input
        4. Summary of string placement
    2. Standard String Operations
      1. Comparing Strings
      2. Copying Strings
      3. Concatenating Strings
    3. Passing Strings
      1. Passing a Simple String
      2. Passing a Pointer to a Constant char
      3. Passing a String to Be Initialized
      4. Passing Arguments to an Application
    4. Returning Strings
      1. Returning the Address of a Literal
      2. Returning the Address of Dynamically Allocated Memory
        1. Returning the address of a local string
    5. Function Pointers and Strings
    6. Summary
  7. 6. Pointers and Structures
    1. Introduction
      1. How Memory Is Allocated for a Structure
    2. Structure Deallocation Issues
    3. Avoiding malloc/free Overhead
    4. Using Pointers to Support Data Structures
      1. Single-Linked List
      2. Using Pointers to Support a Queue
      3. Using Pointers to Support a Stack
      4. Using Pointers to Support a Tree
      5. Summary
  8. 7. Security Issues and the Improper Use of Pointers
    1. Pointer Declaration and Initialization
      1. Improper Pointer Declaration
      2. Failure to Initialize a Pointer Before It Is Used
      3. Dealing with Uninitialized Pointers
    2. Pointer Usage Issues
      1. Test for NULL
      2. Misuse of the Dereference Operator
      3. Dangling Pointers
      4. Accessing Memory Outside the Bounds of an Array
      5. Calculating the Array Size Incorrectly
      6. Misusing the sizeof Operator
      7. Always Match Pointer Types
      8. Bounded Pointers
      9. String Security Issues
      10. Pointer Arithmetic and Structures
      11. Function Pointer Issues
    3. Memory Deallocation Issues
      1. Double Free
      2. Clearing Sensitive Data
    4. Using Static Analysis Tools
    5. Summary
  9. 8. Odds and Ends
    1. Casting Pointers
      1. Accessing a Special Purpose Address
      2. Accessing a Port
      3. Accessing Memory using DMA
      4. Determining the Endianness of a Machine
    2. Aliasing, Strict Aliasing, and the restrict Keyword
      1. Using a Union to Represent a Value in Multiple Ways
      2. Strict Aliasing
      3. Using the restrict Keyword
    3. Threads and Pointers
      1. Sharing Pointers Between Threads
      2. Using Function Pointers to Support Callbacks
    4. Object-Oriented Techniques
      1. Creating and Using an Opaque Pointer
      2. Polymorphism in C
    5. Summary
  10. Index
  11. About the Author
  12. Colophon
  13. Copyright

Product information

  • Title: Understanding and Using C Pointers
  • Author(s): Richard M Reese
  • Release date: May 2013
  • Publisher(s): O'Reilly Media, Inc.
  • ISBN: 9781449344184