Return To Course Outline

Chapter 5   Pointers and Dynamic Memory

Review for Pointers  

Address Operator & Dereference Operator

Arrays are useful for data storage, when you know an exact size for the memory allocation for the array.
However, if the size is not set, an array may waste space or it may run out of space.  Therefore, we use

new
delete
to allocate and deallocate memory spaces as we need it when we need it.

The new operator:  obtain memory from the free storage.
                                It returns an address of variable stored.
                                Return 0(NULL) if there is no enough space.

Note:     new is followed by the type of the address.
             The new operator automatically creates an object of proper size,
             and remains a pointer of the connect type.
--------------------------------------------------------------------------------
On page 229 of Data Structures with C++ using STL, second edition, by William Ford & William Topp:

You can think of the heap as a bank of memory, much like a financial
bank that maintains a reserve of money.  A program can borrow
(allocate) memory from the heap when additional storage space is required
and then pay it back (deallocate) when it is no longer needed.
--------------------------------------------------------------------------------

Click here for an example.
Allocating Memory:

          int * ip;
    ip = new int;       // Tell the computer to allocate memory for an integer variable
    *ip = 7;      // You can't get to the contents of the new variable except through the pointer ip

The above three statements can also be written as
    int *ip = new int(7);



    int A[10]; // Takes 20 bytes.  If all twenty bytes aren't needed every time, why not use dynamic memory?

    int numOfElements;
    cout << "How many numbers do you want to store?" << endl;
    cin >> numOfElements;    // Let the user choose the number of elements he needs
    int * ip;
    ip = new int[numOfELements];   // Allocate just enough space for the array that the user needs

Releasing Memory:
          To free the space for this object, use the delete operator.

    int *value = new int(7);
    delete value;

    int *months = new int[12];
    delete [] months;    //delete the array (note the [])

Example of insert elements in an ascending order list.

Example:

#include<iostream>
#include<string>
using namespace std;
struct vehicle
{
    string make ;
    int year;
    double price;
};
void main()
{
    vehicle *ptr = new vehicle[3];
    (*ptr).make="Ford";
    ptr->year = 2004;
    ptr->price=12000.00;
    ptr++;
    cout<<"Enter the vehicle type you want"<<endl;
    cin>>ptr->make;
    cout<<"Enter vehicle year model"<<endl;
    cin>>ptr->year;
    cout<<"Enter you price qoute"<<endl;
    cin>>ptr->price;
    cout<<"You spend "<< (*ptr).price<<" on "
        <<ptr->year<<" of year "<<ptr->make<<endl;

    ptr--;

    cout<<"I spend "<< (*ptr).price<<" on "
        <<ptr->year<<" of "<<ptr->make<<endl;
    delete [] ptr;
}

Dynamic Class:

Click here to see program 5-1

Click here to see program 5-2

miniVector Class:   click here to see program 5-3

Matrix Class:   click here to see program 5-4