Home Page Icon
Home Page
Table of Contents for
Cover
Close
Cover
by Richard M Reese
Understanding and Using C Pointers
Preface
Why This Book Is Different
The Approach
Audience
Organization
Summary
Conventions Used in This Book
Using Code Examples
SafariĀ® Books Online
How to Contact Us
1. Introduction
Pointers and Memory
Why You Should Become Proficient with Pointers
Declaring Pointers
How to Read a Declaration
Address of Operator
Displaying Pointer Values
Virtual memory and pointers
Dereferencing a Pointer Using the Indirection Operator
Pointers to Functions
The Concept of Null
To NULL or not to NULL
Pointer to void
Global and static pointers
Pointer Size and Types
Memory Models
Predefined Pointer-Related Types
Understanding size_t
Using the sizeof operator with pointers
Using intptr_t and uintptr_t
Pointer Operators
Pointer Arithmetic
Adding an integer to a pointer
Pointers to void and addition
Subtracting an integer from a pointer
Subtracting two pointers
Comparing Pointers
Common Uses of Pointers
Multiple Levels of Indirection
Constants and Pointers
Pointers to a constant
Constant pointers to nonconstants
Constant pointers to constants
Pointer to (constant pointer to constant)
Summary
2. Dynamic Memory Management in C
Dynamic Memory Allocation
Memory Leaks
Losing the address
Hidden memory leaks
Dynamic Memory Allocation Functions
Using the malloc Function
To cast or not to cast
Failing to allocate memory
Not using the right size for the malloc function
Determining the amount of memory allocated
Using malloc with static and global pointers
Using the calloc Function
Using the realloc Function
The alloca Function and Variable Length Arrays
Deallocating Memory Using the free Function
Assigning NULL to a Freed Pointer
Double Free
The Heap and System Memory
Freeing Memory upon Program Termination
Dangling Pointers
Dangling Pointer Examples
Dealing with Dangling Pointers
Debug Version Support for Detecting Memory Leaks
Dynamic Memory Allocation Technologies
Garbage Collection in C
Resource Acquisition Is Initialization
Using Exception Handlers
Summary
3. Pointers and Functions
Program Stack and Heap
Program Stack
Organization of a Stack Frame
Passing and Returning by Pointer
Passing Data Using a Pointer
Passing Data by Value
Passing a Pointer to a Constant
Returning a Pointer
Pointers to Local Data
Passing Null Pointers
Passing a Pointer to a Pointer
Writing your own free function
Function Pointers
Declaring Function Pointers
Using a Function Pointer
Passing Function Pointers
Returning Function Pointers
Using an Array of Function Pointers
Comparing Function Pointers
Casting Function Pointers
Summary
4. Pointers and Arrays
Quick Review of Arrays
One-Dimensional Arrays
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
When a string literal is not a constant
String Initialization
Initializing an array of char
Initializing a pointer to a char
Initializing a string from standard input
Summary of string placement
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
Returning the address of a local string
Function Pointers and Strings
Summary
6. Pointers and Structures
Introduction
How Memory Is Allocated for a Structure
Structure Deallocation Issues
Avoiding malloc/free Overhead
Using Pointers to Support Data Structures
Single-Linked List
Using Pointers to Support a Queue
Using Pointers to Support a Stack
Using Pointers to Support a Tree
Summary
7. Security Issues and the Improper Use of Pointers
Pointer Declaration and Initialization
Improper Pointer Declaration
Failure to Initialize a Pointer Before It Is Used
Dealing with Uninitialized Pointers
Pointer Usage Issues
Test for NULL
Misuse of the Dereference Operator
Dangling Pointers
Accessing Memory Outside the Bounds of an Array
Calculating the Array Size Incorrectly
Misusing the sizeof Operator
Always Match Pointer Types
Bounded Pointers
String Security Issues
Pointer Arithmetic and Structures
Function Pointer Issues
Memory Deallocation Issues
Double Free
Clearing Sensitive Data
Using Static Analysis Tools
Summary
8. Odds and Ends
Casting Pointers
Accessing a Special Purpose Address
Accessing a Port
Accessing Memory using DMA
Determining the Endianness of a Machine
Aliasing, Strict Aliasing, and the restrict Keyword
Using a Union to Represent a Value in Multiple Ways
Strict Aliasing
Using the restrict Keyword
Threads and Pointers
Sharing Pointers Between Threads
Using Function Pointers to Support Callbacks
Object-Oriented Techniques
Creating and Using an Opaque Pointer
Polymorphism in C
Summary
Index
About the Author
Colophon
Copyright
Search in book...
Toggle Font Controls
Playlists
Add To
Create new playlist
Name your new playlist
Playlist description (optional)
Cancel
Create playlist
Sign In
Email address
Password
Forgot Password?
Create account
Login
or
Continue with Facebook
Continue with Google
Sign Up
Full Name
Email address
Confirm Email Address
Password
Login
Create account
or
Continue with Facebook
Continue with Google
Next
Next Chapter
Understanding and Using C Pointers
Add Highlight
No Comment
..................Content has been hidden....................
You can't read the all page of ebook, please click
here
login for view all page.
Day Mode
Cloud Mode
Night Mode
Reset