下载中心
Understanding and Using C Pointers.pdfpdf
1星 发布者: lcofjp

2018-04-14 | 1积分 | 7.34MB |  13 次下载

下载 收藏 评论

文档简介
标签: C

C

Pointer

Pointer

ECU

ECU

汽车电子

汽车电子

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......

评论
相关视频
  • 直播回放: Keysight 小探头,大学问,别让探头拖累你的测试结果!

  • 控制系统仿真与CAD

  • MIT 6.622 Power Electronics

  • 直播回放:基于英飞凌AIROC™ CYW20829低功耗蓝牙芯片的无线组网解决方案

  • 直播回放:ADI & WT·世健MCU痛点问题探索季:MCU应用难题全力击破!

  • Soc Design Lab - NYCU 2023

推荐帖子
精选电路图
  • 家用电源无载自动断电装置的设计与制作

  • PIC单片机控制的遥控防盗报警器电路

  • 短波AM发射器电路设计图

  • 开关电源的基本组成及工作原理

  • 用NE555制作定时器

  • 基于TDA2003的简单低功耗汽车立体声放大器电路

×