CS225,  Fall 2006,  addresses in C++ programs      AddressesinCPP.htm

HOMEWORK: C++ addresses
A.  Run the first program (Variables: addresses and pointers).  Do your memory addresses match mine?  In a complex computer, the addresses we "see" in our application may be in a space renumbered from the actual space, for the purpose of supporting our application.
B. a)Add to the single Array as Pointer program lines that will output the contents and addresses of the rest of the array elements. 
b) What is the offset from the base address for element g[k]?  c) How many bytes is a C++ int?  Is this consistent?

C. What does C++ do if you run "off the end" of an array?
a) Continue with the program of part A.  Add lines that will get the contents of array elements g[3] and g[4], which should not exist, since the array elements should be only indexed 0,1,2.
b) Now see what happens when you try to assign values to array elements which should not exist.  Uncomment the linefor g[3].  Run the program.  Now uncomment the line for g[4] also.  Run the program.  Write down the results.

D.  We saw that global variables get assigned memory spaces counting forward.  Local variables get assigned memory spaces counting backward. The array here is a global variable.  Move it inside main so it becomes a local variable.  Do the array elements go backward, or forward, now?
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
//Variables:  addresses and pointers
#include <iostream>
using namespace std;

int g, h;       //global variables
int main()
{  
     g= 5;
     h= 6;
     int x=10, y=11;  //local variables
     
     int* adrg;  // adrg is a "pointer variable", i.e. an address
     int* adrx;     
     adrg = &g;  //           &g is the address of g.
     adrx = &x;
    cout <<"g = "<< g  <<",  address of g = " << adrg <<endl;
    cout <<"h = "<< h <<",  address of h = " << &h <<endl;
    cout <<"x = "<< x  <<",  address of x = " << adrx <<endl;
    cout << "y = "<< y  <<",  address of y = " << &y <<endl;  
    return 0;
}
// Here are my results, what are yours?
//g = 5,  address of g = 00477848   on my home computer .  Note takes 4 bytes.
//h = 6,  address of h = 0047784C
//x = 10,  address of x = 006AFDF4 old computer, 0013FF7C new computer
//y = 11,  address of y = 006AFDF0 old computer, 0013FF78 new computer
//Note global variables are assigned forward.
// but local variables are assigned backward.

= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =

//Array addressing
#include <iostream>
using namespace std;

int g[3];       //global array variable
int main()

     g[0]= 5;
     g[1]= 6;
     g[2]= 7;
     //g[3]= 8;  //What happens if I do this? (out of declared space)
     //g[4]= 9;  //and this?
     
     int* adrg0;  // adrg is a "pointer variable", i.e. an address
     adrg0 = &g[0];  //           &g[0] is the address of g.
      
    cout <<"g = "<< g  <<",  address of g[0] = " << adrg0 <<endl;
//the array variable in C++ "is" a pointer (an address) to the first element
    cout <<"g[0] = "<< g[0]  <<",  address of g[0] = " << &g[0] <<endl;
    //etc.

   //cout <<"g[3] = "<< g[3]  <<",  address of g[3] = " << &g[3] <<endl; //can I do this without filling g[3]?
    // etc.
    return 0;
}
//g = 00477848,  address of g[0] = 00477848
//g[0] = 5,  address of g[0] = 00477848
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
//Double Array
#include <iostream>
using namespace std;

int g[3][2];       //global array variable
int main()
{  
     g[0][0]= 10;  //g[0] is an array whose elements are a 2-element array.
     g[0][1]= 11; 
     g[1][0]= 12;
     g[1][1]= 13;
     g[2][0]= 14;
     g[2][1]= 15;
   
     int* adrg0;  // adrg0 is a "pointer variable", i.e. an address
     adrg0 = &g[0][0];  //           &g[0] is the address of g.
       
    cout <<"g = "<< g  <<",  address of g[0][0] = " << adrg0 <<endl;
//the array variable in C++ "is" a pointer (an address) to the first element
    cout <<"g[0] = "<< g[0]  <<",  address of g[0] = " << &g[0] <<endl;   
    cout <<"g[0][0] = "<< g[0][0]  <<",  address of g[0][0] = " << &g[0][0] <<endl;   
    cout <<"g[0][1] = "<< g[0][1]  <<",  address of g[0][1] = " << &g[0][1] <<endl;   
    cout <<"g[1] = "<< g[1]  <<",  address of g[1] = " << &g[1] <<endl;   
    cout <<"g[1][0] = "<< g[1][0]  <<",  address of g[1][0] = " << &g[1][0] <<endl;   
    cout <<"g[1][1] = "<< g[1][1]  <<",  address of g[1][1] = " << &g[1][1] <<endl;   
    return 0;
}
//g = 00477848,  address of g[0][0] = 00477848
//g[0] = 00477848,  address of g[0] = 00477848
//g[0][0] = 10,  address of g[0][0] = 00477848
//g[0][1] = 11,  address of g[0][1] = 0047784C
//g[1] = 00477850,  address of g[1] = 00477850
//g[1][0] = 12,  address of g[1][0] = 00477850
//g[1][1] = 13,  address of g[1][1] = 00477854
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =

To Sievers Home Page
CS225-Fall06/AddressesinCPP.htm 
4:00pm, 10/24/06
This page belongs to Sally Sievers who is solely responsible for its content. Please see our statement of responsibility.