Other Sellers on Amazon
Understanding and Using C Pointers: Core Techniques for Memory Management 1st Edition
Enhance your purchase
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
Special offers and product promotions
- Buy 2, save 50% on 1. Offered by Amazon.com Shop items
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 '\0'. 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.
About the Author
Richard Reese has worked in the industry and academics for the past 29 years. For 10 years he provided software development support at Lockheed and at one point developed a C based network application. He was a contract instructor providing software training to industry for 5 years. Richard is currently an Associate Professor at Tarleton State University in Stephenville Texas.
- Publisher : O'Reilly Media; 1st edition (May 28, 2013)
- Language : English
- Paperback : 226 pages
- ISBN-10 : 1449344186
- ISBN-13 : 978-1449344184
- Item Weight : 13 ounces
- Dimensions : 7 x 0.48 x 9.19 inches
- Best Sellers Rank: #237,390 in Books (See Top 100 in Books)
- Customer Reviews:
About the authors
Top reviews from the United States
There was a problem filtering reviews right now. Please try again later.
The book, however, has several flaws. A lot of the material deals with nonstandard libraries. For example, the section on pointers and threads mentions C11 support for threads, but then discusses the POSIX standard rather than going into any details about the C standard. I found this and other references to extensions outside the C standard annoying; a book about C should focus on portable code.
Also, the book is not well organized. Throughout the book there are "forward references" saying that the topic being mentioned will be discussed elsewhere later. This leads to a lot of back-and-forth flipping of pages. For example, pointer arithmetic is explained in Chapter 1, but is used only in Chapter 4; a particular data structure is minimally introduced on page 133, then actually used sixty pages later. The way information is presented and explained could have been more systematic.
The biggest flaw is that some of the code is nonstandard and frankly, quite ugly. For instance:
(1) a function on page 87 uses pointers to memory that has already been deallocated, which is undefined behavior. The same function has redundant variables "length" and "currentPosition" -- one of the two would have been sufficient to accomplish the task.
(2) Elsewhere, Reese defines a linked list header structure with pointers to the head node, the tail node, and the current node. This structure is used at several later points in the book, but the reason for including a pointer to the current node is never made clear; none of the code in the book ever uses it, and indeed, it's difficult to see why it would be useful.
(3) While Reese explains the difference between the value 0 and the NUL character, in several places his code elides the difference, using the former where the latter would be more strictly appropriate (e.g., page 131 has `while (*string != 0)` when `while (*string != '\0')` or preferably `while (*string)` would be better style.
(4) Throughout, the book provides examples of infinite loops with break statements instead of using good structured style and having the break condition be the loop conditional.
(5) Surprisingly for a book that mentions the C99 and C11 standards often, there are a lot of references to using Microsoft Visual C++ as a compiler. Microsoft does not, and by all accounts never will, support C standards past C89. It is a C++ compiler, not a current C compiler. It mandates practices that are not C like in their philosophy, and Reese's code succumbs. For instance, he explicitly casts the return values from malloc and realloc to pointers of the appropriate type, which is not considered good C style. Good C code should be written in compliance to the C standard, not in conformance with the requirements imposed by a compiler geared to a different language.
The chronically problematic sample code is a serious shortcoming, and would be quite misleading for relatively new programmers. But then, the book is more a review of programming techniques using pointers than an introduction to them. It is geared more towards intermediate or experienced programmers than towards novices. For someone who has enough experience to be able to refactor the code properly, the book is a very useful review.
int *pi; // means "pi is a pointer to an int"
It clearly explains the use of the dereference operator in an lvalue:
*pi = 5 // The int 5 is assigned to memory location where pi points.
But it pointed out that this (very similar to the above) is a common error:
int *pi = 5; // We are declaring a pointer to an int, and then trying to assign the integer 5 to the pointer, not the integer memory location.
And that's all just in chapter 1! He helpfully diagrams memory after each use of pointers, making the difference between the stack, heap and the data segment (static memory) clear. There are detailed chapters on pointer use with dynamic memory, functions, arrays, strings, and structures.
It's nice to have an entire book focused entirely on pointers. The focused drill is clearing up confusion that plagued me for longer than I care to admit. Thank you!
A really happy Michael Stolzle,
All too common with math and theoretical CS authors, many or most of whom are professional educators, is the attitude that brevity is beauty. Whether they are interested in instilling their ideas in the reader is not evident from their writing. Reese provides the right level of detail and a coherent exposition which develops a solid intuition without having to re-read his explanations.
The conceptual diagrams are helpful but a little ambiguous in referencing which rectangle is the function stack, which is main, and which is the heap. I hope the author improves the diagrams in his next edition. I do not deduct any stars because this book is much better than anything else, and it covers essential CS material.
Top reviews from other countries
It skips all the other stuff covered by intro books and focuses on the real advantage of using C over other languages (i.e. pointers). I would recommend this book for new programmers or just as a refresher to others (as in my case).