Understanding and Using C Pointers
《深入理解C指针》的英文版。
(C/C++程序员进阶必备,透彻理解指针与内存管理)
Preface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ix
1. Introduction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Pointers and Memory 2 Why You Should Become Proficient with Pointers 3 Declaring Pointers 5 How to Read a Declaration 7 Address of Operator 8 Displaying Pointer Values 9 Dereferencing a Pointer Using the Indirection Operator 11 Pointers to Functions 11 The Concept of Null 11
Pointer Size and Types 15 Memory Models 16 Predefined Pointer-Related Types 16
Pointer Operators 20 Pointer Arithmetic 20 Comparing Pointers 25
Common Uses of Pointers 25 Multiple Levels of Indirection 25 Constants and Pointers 27
Summary 32
2. Dynamic Memory Management in C. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
Dynamic Memory Allocation 34 Memory Leaks 37 Dynamic Memory Allocation Functions 39 Using the malloc Function 39 Using the calloc Function 43
Using the realloc Function 44
The alloca Function and Variable Length Arrays 46 Deallocating Memory Using the free Function 47 Assigning NULL to a Freed Pointer 48 Double Free 48 The Heap and System Memory 50 Freeing Memory upon Program Termination 50 Dangling Pointers 51 Dangling Pointer Examples 51 Dealing with Dangling Pointers 53 Debug Version Support for Detecting Memory Leaks 54 Dynamic Memory Allocation Technologies 54 Garbage Collection in C 55 Resource Acquisition Is Initialization 55 Using Exception Handlers 56 Summary 56
3. Pointers and Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
Program Stack and Heap 58 Program Stack 58 Organization of a Stack Frame 59
Passing and Returning by Pointer 61 Passing Data Using a Pointer 62 Passing Data by Value 62 Passing a Pointer to a Constant 63 Returning a Pointer 64 Pointers to Local Data 66 Passing Null Pointers 67 Passing a Pointer to a Pointer 68
Function Pointers 71 Declaring Function Pointers 72 Using a Function Pointer 73 Passing Function Pointers 74 Returning Function Pointers 75 Using an Array of Function Pointers 76 Comparing Function Pointers 77 Casting Function Pointers 77
Summary 78
4. Pointers and Arrays. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
Quick Review of Arrays 80 One-Dimensional Arrays 80
iv | Table of Contents
Two-Dimensional Arrays
Multidimensional Arrays Pointer Notation and Arrays
Differences Between Arrays and Pointers
Using malloc to Create a One-Dimensional Array Using the realloc Function to Resize an Array Passing a One-Dimensional Array
Using Array Notation
Using Pointer Notation
Using a One-Dimensional Array of Pointers Pointers and Multidimensional Arrays
Passing a Multidimensional Array
Dynamically Allocating a Two-Dimensional Array
Allocating Potentially Noncontiguous Memory
Allocating Contiguous Memory Jagged Arrays and Pointers Summary
5. Pointers and Strings. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
String Fundamentals String Declaration The String Literal Pool String Initialization
Standard String Operations Comparing Strings Copying Strings Concatenating Strings
Passing Strings
Passing a Simple String
Passing a Pointer to a Constant char Passing a String to Be Initialized Passing Arguments to an Application
Returning Strings
Returning the Address of a Literal
Returning the Address of Dynamically Allocated Memory
Function Pointers and Strings Summary
6. Pointers and Structures. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Introduction
How Memory Is Allocated for a Structure
Structure Deallocation Issues
Avoiding malloc/free Overhead 139 Using Pointers to Support Data Structures 141 Single-Linked List 142 Using Pointers to Support a Queue 149 Using Pointers to Support a Stack 152 Using Pointers to Support a Tree 154 Summary 158
7. Security Issues and the Improper Use of Pointers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159
Pointer Declaration and Initialization 160 Improper Pointer Declaration 160 Failure to Initialize a Pointer Before It Is Used 161 Dealing with Uninitialized Pointers 162
Pointer Usage Issues 162 Test for NULL 163 Misuse of the Dereference Operator 163 Dangling Pointers 164 Accessing Memory Outside the Bounds of an Array 164 Calculating the Array Size Incorrectly 165 Misusing the sizeof Operator 166 Always Match Pointer Types 166 Bounded Pointers 167 String Security Issues 168 Pointer Arithmetic and Structures 169 Function Pointer Issues 170
Memory Deallocation Issues 172 Double Free 172 Clearing Sensitive Data 173
Using Static Analysis Tools 173 Summary 174
8. Odds and Ends. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175
Casting Pointers 176 Accessing a Special Purpose Address 177 Accessing a Port 178 Accessing Memory using DMA 179 Determining the Endianness of a Machine 180
Aliasing, Strict Aliasing, and the restrict Keyword 180 Using a Union to Represent a Value in Multiple Ways 182 Strict Aliasing 183 Using the restrict Keyword 184
Threads and Pointers 185
Sharing Pointers Between Threads
Using Function Pointers to Support Callbacks Object-Oriented Techniques
Creating and Using an Opaque Pointer
Polymorphism in C Summary
Index. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
文档内容节选
Understanding and Using C Pointers Richard Reese Understanding and Using C Pointers by Richard Reese Copyright 2013 Richard Reese PhD All rights reserved Printed in the United States of America Published by OReilly Media Inc 1005 Gravenstein Highway North Sebastopol CA 95472 OReilly books may be purchased for educational business or sales promotional use Online editions are also available for most titles httpmysafaribooksonlinecom For more information contact our corporate institutional sales d......