CTEC1335/2008F Updated: Mon 10 Nov 2008 11:52:18 EST Pointers -------- A pointer is a variable that stores a memory address. Up to now, we have been using simple types: bool char int double string istream ostream You can declare pointer variables that will contain the memory addresses of data with these particular types. bool * char * int * double * string * istream * ostream * The relationship between data variables and pointer variables is as follows: - if "bool * pb" is a pointer to a bool, then "*pb" is a bool - if "char * pc" is a pointer to a char, then "*pc" is a char - if "int * pi" is a pointer to an int, then "*pi" is an int - if "double * pd" is a pointer to a double, then "*pd" is a double - if "string * pstr" is a pointer to a string, then "*pstr" is a string --- Like data variables, pointer variables must be initialized, so that they contain a valid memory address. (A valid address is one that the operating system has authorized the program to use, and which refers to sufficient space to one or more of that data.) Two ways of initializing pointer variables: 1. Use the "new" operator: it allocates memory from the operating system in a special area of memory called the "heap", and returns the address of that memory. This address is stored in the pointer variable. "new" can be used to allocate enough memory for a single data item, or multiple data -- i.e., an array of data. e.g. int * pi ; // step 0. declare pointer variable pi = new int ; // step 1. initialize pointer *pi = 1 ; // step 2. initialize data at pointer char * line ; // step 0. line = new char [ 80 ] ; // initialize pointer; // allocate enough memory // for an array of 80 chars cin.getline( line, 80 ) ; // initialize array // (alternate form of // getline function) 2. The the "address-of" operator (&): it returns the memory address of an existing data variable. This address is stored in the pointer variable. int x = 1234 ; // steps 0 and 1. // declare and initialize data variable int * px = & x ; // steps 2 and 3. // declare and initialize pointer // variable cout << x << endl ; // prints "1234" cout << *px << endl ; // also prints "1234" *px = 4321 ; // changes *px, which also changes x! cout << x << endl ; // now, prints "4321" cout << *px << endl ; // also prints "4321" For data variables, there are always two steps involved: 1. Declare the data variable: give a type and a variable name. 2. Initialize the data variable, usually with an assignment statement. From the above examples, when you use pointer variables, at least two additional steps are required: 3. Declare the pointer variable: give a type, *, and a variable name. 4. (a) Initialize the pointer variable using "new". In this case, you need to initialize the data, similar to step 2, but through the pointer variable. (b) Initialize the pointer variable using "&". In this case, step 1 must have already been completed. You can either do step 2 before or after, or initialize the data through the pointer. Memory Addresses ---------------- All variables and functions in a C++ program have an address. The locations are organized by the compiler, linker and operating system, and often depend on the order that the variables/functions appear. Each data variable declared inside a function, either as a function parameter/argument or as a local variable is allocated by the operating system in a special memory area called the "stack" (also known as the "stack segment".) When the function returns, these variables are automatically cleaned off the stack. Stack addresses are decreasing in number. When you allocate memory using the new operator, the memory is allocated on the heap. Heap addresses are increasing in number. Functions are stored in a special memory area called the "code segment." Global variables and "static" variables are stored in a special memory address called the "data segment" (which often includes the heap as well.) Constants are also stored in part of the data segment. Pointer variables not only store an address, but they also have their own address, as they declared as local or global/static variables! --- See http://en.wikipedia.org/wiki/Pointer http://en.wikipedia.org/wiki/Reference_(C%2B%2B)