Understanding and Using C Pointers

Understanding and Using C Pointers

Language: English

Pages: 226

ISBN: 1449344186

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

Hands-On Mobile App Testing: A Guide for Mobile Testers and Anyone Involved in the Mobile App Business

Functional Programming in Scala

Mastering Cloud Computing: Foundations and Applications Programming

LabVIEW Graphical Programming Cookbook

Credibilistic Programming: An Introduction to Models and Applications (Uncertainty and Operations Research)

Multicore Technology: Architecture, Reconfiguration, and Modeling (Embedded Multi-Core Systems)

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Losing the address of memory is illustrated in the following code sequence where pi is reassigned a new address. The address of the first allocation of memory is lost when pi is allocated memory a second time. int *pi = (int*) malloc(sizeof(int)); *pi = 5; ... pi = (int*) malloc(sizeof(int)); This is illustrated in Figure 2-3 where the before and after images refer to the program’s state before and after the second malloc’s execution. The memory at address 500 has not been released, and the.

String literal pool String literals are frequently allocated to read-only memory. This makes them immutable. It doesn’t matter where a string literal is used or whether it is global, static, or local. In this sense, string literals do not have scope. When a string literal is not a constant In most compilers, a string literal is treated as a constant. It is not possible to modify the string. However, in some compilers, such as GCC, modification of the string literal is possible.

Function with 0 instead of calling the function and comparing its return value to 0. Remember, when a function name is used by itself, it returns the address of the function. A similar mistake is using a function return value directly without comparing its result to some other value. The address is simply returned and evaluated as true or false. The address of the function is not likely to be zero. As a result, the address returned will be evaluated as true since C treats any nonzero value as.

Figure 8-1, where address 8 is not in the application’s address space. If the pointer is dereferenced, it will normally result in the application’s termination. Figure 8-1. Casting an integer to a bad location For some situations, such as when we need to address memory location zero, we may need to cast a pointer to an integer and then cast it back to a pointer. This is more common on older systems where a pointer’s size is the same size as an integer. However, this does not always work.

This data to compute the factorial, store the answer in the result field, call the callback function, and then terminate the thread: typedef struct _factorialData { int number; int result; void (*callBack)(struct _factorialData*); } FactorialData; void factorial(void *args) { FactorialData *factorialData = (FactorialData*) args; void (*callBack)(FactorialData*); // Function prototype int number = factorialData->number; callBack = factorialData->callBack; int num = 1; for(int i = 1; i<=number;.

Download sample

Download