Understanding and Using C Pointers
- Length: 226 pages
- Edition: 1
- Language: English
- Publisher: O'Reilly Media
- Publication Date: 2013-05-15
- ISBN-10: 1449344186
- ISBN-13: 9781449344184
- Sales Rank: #115380 (See Top 100 Books)
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
Q&A with Richard Reese, author of “Understanding and Using C Pointers”
Q. Why is your book timely?
A. The C language continues to be a popular and widely used language. In addition, there are millions of lines of C code being maintained. Pointers are central to C and a solid understanding of them is important in crafting reliable and secure software. Understanding C pointers has always been important but they have not always been adequately covered. Their widespread usage in new and old applications begs a complete and comprehensive coverage.
Q. What do you hope that readers of your book will walk away with?
A. I hope that the experienced developer will be able to fill any holes in their understanding of C pointers. I hope that the less experienced developers will become more proficient and confident in their application of pointers.
Q. Can you give us a few tips on using pointers?
- Be careful when using pointers to void. If you cast a pointer to a pointer to void there is nothing preventing you from casting it to a different pointer type.
- When using function pointers the programmer must be careful to ensure it is used properly because C does not check to see if the correct parameters are passed.
- New code development should use the restrict keyword with most pointer declarations. This will enable better code optimization. Modifying existing code may not be worth the effort.
- Compilers do not always do a good job at reporting alias-related warnings. They can sometimes miss aliases and may sometimes report alias problems where they don’t exist. It is ultimately up to the programmer to identify alias conditions.
- Remember NULL and NUL are different. NULL is used as a special pointer and is typically defined as ((void*)0). NUL is a char and is defined as ‘�’. They should not be used interchangeably.
- Improperly aligned pointers can result in an abnormal program termination or retrieval of bad data. In addition, slower pointer access is possible if the compiler is required to generate additional machine code to compensate for the improper alignment.
Table of Contents
Chapter 1. Introduction
Chapter 2. Dynamic Memory Management in C
Chapter 3. Pointers and Functions
Chapter 4. Pointers and Arrays
Chapter 5. Pointers and Strings
Chapter 6. Pointers and Structures
Chapter 7. Security Issues and the Improper Use of Pointers
Chapter 8. Odds and Ends