/**************************************************************/ // R. A. Hillyard // pointer03.cpp // November 2001 // // Program to demonstrate the use of pointers, new, and delete /**************************************************************/ #include<iostream> using namespace std; int main() { int *intPtr1, *intPtr2; //allocate new memory and point to it intPtr1 = new int; intPtr2 = new int; //print out value of uninitialized memory locations cout << "intPtrs address: " << intPtr1 << " " << intPtr2 << endl; cout << "intPtrs value : " << *intPtr1 << " " << *intPtr2 << endl << endl; //set the value using the defererencing operator *intPtr1 = 33; *intPtr2 = 22; cout << "intPtr1: " << intPtr1 << " " << *intPtr1 << endl; cout << "intPtr2: " << intPtr2 << " " << *intPtr2 << endl << endl; //lost reference problem - what intPtr1 was //pointing to is now "lost" intPtr1 = intPtr2; cout << "intPtr1: " << intPtr1 << " " << *intPtr1 << endl; cout << "intPtr2: " << intPtr2 << " " << *intPtr2 << endl << endl; //reset pointers for next example intPtr1 = new int; //give back memory when finished delete intPtr2; intPtr2 = intPtr1; *intPtr1 = 77; cout << "intPtrs: " << intPtr1 << " " << *intPtr1 << " " << *intPtr2 << endl; //give back memory when finished delete intPtr1; return 0; } /*********************Program Output***************************/ intPtrs address: 0x00133560 0x00133570 intPtrs value : 1258860 1258876 intPtr1: 0x00133560 33 intPtr2: 0x00133570 22 intPtr1: 0x00133570 22 intPtr2: 0x00133570 22 intPtrs: 0x001335a0 77 77