// mem.C -- illustrate memory addresses, layout of C++ program #include #include double pi = 3.14; void fun1(int, int, int, int, int, int, int, int, int, int); void fun2(int); void fun3(int); int main() { int a, b, c[257], *p = new int; cout << "starting main()\n"; cout << hex; cout << " address of pi " << setw(8) << &pi << endl; cout << " address of a " << setw(8) << &a << endl; cout << " address of b " << setw(8) << &b << endl; cout << " address of c[0] " << setw(8) << &c[0] << endl; cout << " address of c[256] " << setw(8) << &c[256] << endl; cout << " address of pointer p " << setw(8) << &p << endl; cout << " address value in p " << setw(8) << p << endl; cout << " address of fun1 " << setw(8) << (unsigned) fun1 << endl; cout << " address of fun2 " << setw(8) << (unsigned) fun2 << endl; cout << " address of fun3 " << setw(8) << (unsigned) fun3 << endl; fun1(1,2,3,4,5,6,7,8,9,10); return 0; } void fun1(int a, int b, int c, int d, int e, int f, int g, int h, int i, int j) { int *p = new int; cout << "starting fun1()\n"; cout << " address of param a " << setw(8) << &a << endl; cout << " address of param j " << setw(8) << &j << endl; cout << " address value in p " << setw(8) << p << endl; fun2(a+b+c+d+e+f+g+h+i+j); } void fun2(int n) { int a[65536], *p = new int[65536]; cout << "starting fun2()\n"; cout << " address of param n " << setw(8) << &n << endl; cout << " address of local a[0] " << setw(8) << &a[0] << endl; cout << " address of a[65535] " << setw(8) << &a[65535] << endl; cout << " address value in p " << setw(8) << p << endl; cout << " address of p[65535] " << setw(8) << &p[65535] << endl; fun3(16); } void fun3(int n) { int a[16], *p = new int[n]; cout << "starting fun3()\n"; cout << " address of param n " << setw(8) << &n << endl; cout << " address of local a[0] " << setw(8) << &a[0] << endl; cout << " address value in p " << setw(8) << p << endl; }