cp ~cpsc/public_html/Spring2010/CPSC270A/lab2/*.cc .
    Node myNode;
would declare myNode to be a reference (currently null) to a Node
object.  The actual object would be created using the new operator.
In C++, the above declaration would create the actual Node (no
new would be needed). 
To declare a reference (pointer) to an object we use *. The following declaration declares myNodePtr as a pointer to a Node:
     Node * myNodePtr;
The actual node would be constructed using the new operator as follows:
     myNodePtr = new Node;
You can also use * and new on primitive data types such as int. The following allocates space in memory for an int and assigns intPtr to point to it.
int * intPtr = new int;To access the actual value of the integer you need to use the operator * to dereference the pointer - *intPtr is the value of the integer in the location pointed to by intPtr.
The & operator gives the address of a variable. Some examples using * and &:
    myNodePtr = &myNode;  
    int * numPtr = new int;
    int num = 5;
    cout << &num << " an address!";  //prints the address of num
    *numPtr = 13;   // *numPtr is the contents of the location pointed
                    // to by numPtr - * is "dereferencing" numPtr
 
The program PointerFun.cc illustrates some of the properties of the & ("address of") operator and the * operator (when used to declare and when used to dereference a pointer variable). Study it then compile and run it.
      int * a = new int[n];
The program DynamicArray.cc illustrate this. Try it.
Problems with memory when using dynamic allocation: In C++ space allocated remains allocated to your process unless you explicitly free it. There is no automatic garbage collection as there is in Java. The delete command is used to place the memory allocated to a pointer back on free store.
     delete numPtr;   // returns the space for the int pointed to by numPtr
     delete [] a;     // frees the space for the array pointed to by a
The program ArrayMemory.cc illustrates the problem with allocating lots of space without freeing it. Follow the instructions below to run the program, examining memory as it runs. The program pauses for input so you can see how much memory is being used.
       ps aux | grep yourExecutable
To pass a parameter by reference in C++ you use the ampersand in the header for the function. So, the following would pass the first two parameters by reference and the third one by value:
     void doStuff (int& a, int& b, int c)
     {
        c = c * 2;
        a = a + c;
        b = b - c;
     }
A call to the function would just use the int variables for the
actual parameters - the address of the first two would be passed.
For example,
       int m = 150;
       int n = 100;
       int p = 5;
       doStuff(m,n,p);
Note: It is standard practice to just list the type of each parameter
in the declaration (when it is separate from the definition) -
in this example you would put
      void doStuff (int&, int&, int)
This code is in ReferenceParameters.cc. Run it to make sure you understand what happens and how reference and value parameters differ.
The program also uses the function strcpy to copy one string to another. Note that the first argument is the destination, the second is the string to be copied. This function (or a loop where you copy character by character) should be used when you need to copy. You cannot use assignment.
Compile and run the program. Note that for the string literal the number of bytes is one more than the number of characters - that accounts for the null character. For the string you typed in, the number of bytes is the number declared for the array.
Exercise:
If you didn't already do it run the program above again entering a string with more than one word. Do it again putting several white space characters in front of the sentence. What happened?
You should have noticed that cin skips over whitespace and stops reading at whitespace. So it doesn't work to read a whole line that may contain spaces. To do that you need to use one of the functions get or getline that operate on cin.
The program ReadStrings.cc reads in two lines of text and prints them. Note the alternate syntax for using get and getline.
Exercise:
The point - if you create your own string character by character you must explicitly add the null character to the end.