1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
|
#include <iostream>
#include <iomanip>
using namespace std;
class Number { // modify class Number if desired to provide constructors, etc.
public:
string name; // name of the Number
Number *next; // pointer to the next Number in the list
};
#define show_addr_value(var, width) \
cout<<"address of " <<setw(width)<<left<<#var<<" is: &"<<&var<<" " \
<<"contents of "<<setw(width)<<left<<#var<<" is: "<<var<<endl;
#define show_addr(var, width) \
cout<<"address of " <<setw(width)<<left<<#var<<" is: &"<<&var<<endl;
int main () {
cout << "Output from Lab10 memory diagram on pointers:\n"
<<"---------------------------------------------\n";
float price = 0;
float *p_price = &price;
*p_price = 18.65;
show_addr_value(price, 8);
show_addr_value(p_price, 8);
cout << "\nThe contents of *p_price is: " << *p_price << endl;
// contents of pi
cout << "<========================" << endl;
double PI = 3.141592;
double *p_PI = &PI;
double PI_div_2 = *p_PI/2;
show_addr_value(PI, 8);
show_addr_value(PI_div_2, 8);
show_addr_value(p_PI, 8);
cout << "\nThe contents of *p_PI is: " << *p_PI << endl;
// acceid
cout << "<========================" << endl;
int ACCeID = 1234567;
int *p_ACCeID = &ACCeID;
show_addr_value(ACCeID, 8);
show_addr_value(p_ACCeID, 8);
cout << "\nThe contents of *p_ACCeID is: " << *p_ACCeID << endl;
//
cout << "<========================" << endl;
int *p_amount;
p_amount = new int;
*p_amount = 2468;
show_addr_value(p_amount, 8);
cout<<endl;
cout << "The contents of *p_amount is: " << (dec) << *p_amount << endl;
delete p_amount;
cout << "After delete, the contents of p_amount is: " << p_amount << endl;
p_amount = nullptr;
cout << "After reset to nullptr, the contents of p_amount is: " << p_amount << endl;
//
cout << "<========================" << endl;
int *pArray, arraySize = 3;
pArray = new int [arraySize];
for(int count =0, val=100; count < arraySize; count++, val+=100)
{
pArray[count] = val;
}
show_addr_value(pArray, 8);
show_addr_value(pArray[0], 8);
show_addr_value(pArray[1], 8);
show_addr_value(pArray[2], 8);
delete [] pArray;
cout<<endl;
cout << "After delete [], the contents of pArray is: " << pArray << endl;
pArray = nullptr;
cout << "After reset to nullptr, the contents of pArray is: " << pArray << endl;
//
cout << "<========================" << endl;
int *pArray2, arraySize2 = 3;
pArray2 = new int [arraySize2];
for(int count =0, val=1111; count < arraySize; count++, val+=1111)
{
pArray2[count] = val;
}
show_addr_value(pArray2, 8);
show_addr_value(pArray2[0], 8);
show_addr_value(pArray2[1], 8);
cout << endl;
cout << "No need to delete Array2, it is on the stack, not the heap" << endl;
//
cout << "<========================" << endl;
int *big_buffer;
big_buffer = new int;
show_addr_value(big_buffer, 8);
delete big_buffer;
cout<< endl;
cout << "After delete [], the contents of big_buffer is: " << big_buffer << endl;
big_buffer = nullptr;
cout << "After reset to nullptr, the contents of big_buffer is: " << big_buffer << endl;
//
cout << "<========================" << endl;
int emptyList=0;
show_addr_value(emptyList, 8);
//
cout << "<========================" << endl;
cout<<"static (uses: Number zed):\n";
Number zed;
show_addr(zed, 8);
zed.name = "zero";
show_addr_value(zed.name, 8);
zed.next = 0;
show_addr_value(zed.next, 8);
//
cout << "<========================" << endl;
cout<<"dynamic (uses: p_natural, Number(\"one\"), Number(\"two\"):\n";
int *p_natural;
show_addr_value(p_natural, 8);
return 0;
} // end of main
| |