Home Robotics C++ Physics II AP Physics B Electronics AP Java Astronomy Independent Study Summer Session Contests  About
                                                       

Pointers

 

 




Pointer Variables
 

A pointer variable is a variable that stores a different kind of value, an address.
 
The random access memory of a computer is mapped with locations called addresses. 
But instead of 123 Main Street, memory addresses are labeled with hexadecimal numbers like F5A2 (for representation purposes only - the computer stores them, as with everything else, as a binary number). 
 
Indirection is a technique where information directs you to the actual location of an object.  This concept of indirection is what makes pointer variables difficult to understand at first. 
 
Suppose you are driving from San Jose to San Francisco on Interstate 280.  When a  large sign stating “San Francisco” appears above the road, it is a pointer directing you to the object you are seeking.  The sign is not the city of San Francisco, it is a method of indirection pointing you to your destination.
 
A pointer variable is like a traffic sign.  It will provide information about where data is stored.  It points to a memory location.

 

Syntax

 

A pointer variable is declared by using the asterisk operator (*) with already defined or existing types.  In a declaration, the asterisk operator (*) must precede the identifier being declared as a pointer.
 
     int  *a, *b;                             // a and b are pointer variables 
 
Each of the variables a and b will not directly store integers.  They will store the addresses where an integer is stored in memory.
 
A declared pointer in C++ is uninitialized and therefore contains a garbage address value.  To use such garbage addresses in a program is to invite disaster and usually esults in a program crash.
 
Pointer variables are often used in conjunction with the address operator (&) which returns the address of a variable. 
 

Memory Allocation

 

Dynamic

 

Refers to change. 

A program which uses dynamic memory is one which adjusts the amount of memory used depending on the changing needs of a program. 

For example, a small word processing document needs little memory. 
As the document gets longer, the word processing program allocates more memory to store the growing number of characters.
 
Heap

 

When a C++ program is run, a certain segment of RAM is available for the program to use. 
This stack of memory is called a heap. 
Memory is allocated for use from the top of the heap.
 
new keyword
 
The new command allocates memory for use with pointer variables. 
The general form of the new command is the following:
 
         new  type
 
This command will return a block of memory large enough to hold an object with the specification of type
If the allocation is successful, the operation returns the starting address of a block of memory, otherwise it returns the NULL address 0.
The new command is applied to a specific type and the memory block returned is stored in a pointer variable. 

 

Example using new

 

The code
 
         int  *a;                        //a is declared to be a pointer variable
   a = new int;             //a stores the address of an integer variable
 
         allocates a block of memory (address D2F6 in the example) to store an integer and returns
         that address to a.  It is in this address that the integer value will eventually be stored.
 
The indirection operator
 
To store an integer in this newly allocated memory location requires the use of the indirection (asterisk) operator, *. Also called dereferencing...
 
        *a = 6
 
;
 
Comments on the example
 
The memory location allocated is D2F6.
The pointer variable a stores a memory location, D2F6.
The indirection operator (*) performs a dereference. 
In a sense, the pointer variable a is a reference variable for another address. 
However that address has no other variable name. 
The statement (*a) means to dereference a and work directly with the memory location that was aliased.
 
Additional comments on (*a)
 
Another way of thinking about the statement (*a) is "to inspect the memory location referenced by a."
This is an example of assignment to a location through a pointer variable.

 

Concluding remarks

 

The above is indirection in action. 
The pointer variable a does not store an integer; it redirects you to where the desired information is located.
We do not need to know the actual memory locations although C++ will allow us to print out such information. 
The statement
   
         cout << a;
 
will print out the address D2F6 if applied to the above diagram