// Chapter 3 - Program 2 #include struct date { int month; int day; int year; }; main() { int index, *point1, *point2; point1 = &index;//assigns the address of index to point1. *point1 = 77;//assigns the value of 77 to index. point2 = new int;//allocate a new int pointer *point2 = 173;//assigns a value to the new pointer cout << "The values are " << index << " " << *point1 << " " << *point2 << "\n"; point1 = new int;//allocating a new pointer, and wiping out old point1 point2 = point1;//assigns a new valu to point2, wasting the allocated space on //old point2. *point1 = 999; cout << "The values are " << index << " " << *point1 << " " << *point2 << "\n"; //delete can *only* delete allocation if assigned with "new". delete point1;// point1 deallocated, freeing up that heap space, but point2 is //still pointing to that space! not good procedure. float *float_point1, *float_point2 = new float; float_point1 = new float; *float_point2 = 3.14159; *float_point1 = 2.4 * (*float_point2); delete float_point2; delete float_point1; //a structure pointer: date *date_point; date_point = new date; date_point->month = 10; date_point->day = 18; date_point->year = 1938; cout << date_point->month << "/" << date_point->day << "/" << date_point->year << "\n"; delete date_point; char *c_point; c_point = new char[37]; delete c_point; c_point = new char[sizeof(date) + 133]; delete c_point; } // Result of execution // // The values are 77 77 173 // The values are 77 999 999 // 10/18/1938