Pointers

Chris Wild

Last modified: Dec 26, 2016
Contents:

1 Pointers

1.1 Description

1.2 Example

 

  int *P, *Q; // P and Q are of type "pointer to integer"
  int     X; // X is of type "integer";
// Here is a "picture" of memory after these two definitions 
//   are done.
// "?" means the value is "undefined" 

 

   P = &X; // The address-of operator sets the value of P to 
           //  the address of X

// In the following picture, an arrow is used in place of the 
// machine address.
// The arrow means that the value stored in variable P is the 
// machine address of X. Thins value can be used to access
// variable X.
// 
// Now there are two ways to access the value of "X"
// 1) Use the programmer assigned name ("X")
// 2) Use the pointer value (the dereferencing operator gets
//     the value pointed to, which is the value of "X"

 

   *P = 6;
// "*P" is the value of variable X, hence this sets X to 6
// another way to do the same thing is
// X = 6;

// To print out the value of X you could use either of the following two statements
   cout << X;
   cout << *P;


//You can use the address-of operator to initialize a pointer (as shown above).
// Or you can use the value of another pointer. Shown below:
   Q = P;
// now both Q and P have the same value - the address of X.
// Thus it is said that both Q and P point at the same variable.

1.3 Tips

*P = &X; // *P is of type "integer", "&X" is of type pointer to integer
Q = *P: // *P is of type integer, Q is of type pointer to integer
Q = &P; // &P is of type pointer to pointer to integer but Q is just of type pointer to integer

2 new and delete

2.1 Description

2.2 Example

int *P = NULL; // define an initially empty pointer
P = new int; // allocates memory for a new integer and assigns its location to "P"
delete P; // returns the memory allocated by the new integer back to the memory pool

P = new int[100]; // allocates a array of 100 integers and assigns its location to "P"
delete [ ] P; // returns the entire array to the memory pool

2.3 Tips

int * P = NULL;

P = new int; // another new integer delete P; P = NULL;

3 Dangling Pointers and Memory Leaks

3.1 Description

Because pointers provide access a memory location and because data and executable code exist in memory together, misuses of pointers can lead to both~ bizarre effects and very subtle errors.

The main problems with pointers are uninitialized pointers, memory leaks and dangling pointers.

3.2 Example

int  *P; // P is uninitialized - could be pointing anywhere
*P = 3; // Use of uninitialized pointer - could cause core dump, illegal access to memory
            // or worse an unexpected change to a random spot in memory

P = new int; // allocate a new integer and assigns a pointer to "P"
P = NULL: // the new integer is now orphaned - nobody can get to it
                  // this is amemory leak

P = new int; // another new integer
delete P; // which is correctlly returned to the system
          //   but now P is a _dangling_pointer_ since it still points to
          //   the now deleted integer


3.3 Tips

int *P = NULL;

P = new int; // another new integer
   <:\smvdots{}:>
delete P;
P = NULL; // otherwise P would be a dangling pointer

4 The Relationship Between Pointers and Arrays

4.1 Description

4.2 Example

const int ARRAY_SIZE = 50;
float A[ARRAY_SIZE];

float *ptr; // declare a pointer to a floating point number
ptr = &A[25]; // point near the middle of array A

4.3 Tips