Understanding and Using C Pointers
Format: PDF / Kindle (mobi) / ePub
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
25; However, if we attempt to initialize cpi to the constant limit as shown below, we will get a warning: const int limit = 500; int *const cpi = &limit; The warning will appear as follows: warning: initialization discards qualifiers from pointer target type If cpi referenced the constant limit, the constant could be modified. This is not desirable. We generally prefer constants to remain constant. Once an address has been assigned to cpi, we cannot assign a new value to cpi as shown
declaration backward allows us to progressively understand the declaration (Figure 1-3). Figure 1-3. The backward declaration Many programmers find that reading the declaration backward is less complex. Note When working with complex pointer expressions, draw a picture of them, as we will do in many of our examples. Address of Operator The address of operator, &, will return its operand’s address. We can initialize the pi pointer with the address of num using this
determined at runtime. Chapter 7. Security Issues and the Improper Use of Pointers Few applications exist where security and reliability are not significant concerns. This concern is reinforced by frequent reports of security breaches and application failures. The responsibility of securing an application largely falls on the developer. In this chapter, we will examine practices to make applications more secure and reliable. Writing secure applications in C can be difficult because of
%x Value: %x\n",&pi, pi); printf("Address of pi: %o Value: %o\n",&pi, pi); printf("Address of pi: %p Value: %p\n",&pi, pi); This will display the address and contents of pi, as shown below. In this case, pi holds the address of num: Address of pi: 4520824 Value: 4520836 Address of pi: 44fb78 Value: 44fb84 Address of pi: 21175570 Value: 21175604 Address of pi: 0044FB78 Value: 0044FB84 The %p specifier differs from %x as it typically displays the hexadecimal number in uppercase. We will use the
from program address space previously used to hold sensitive data. The following sequence illustrates zeroing out of sensitive data in a program: char name; int userID; char *securityQuestion; // assign values ... // Delete sensitive information memset(name,0,sizeof(name)); userID = 0; memset(securityQuestion,0,strlen(securityQuestion)); If name has been declared as a pointer, then we should clear its memory before we deallocate it, as shown below: char *name = (char*)malloc(...); ...