| |
Main Menu | Next Section |
In the last chapter we learned about dynamic memory, and we discovered
that one can request memory just when one needs it. Using this,
we can now write code to increase the size of our list of contracts
whenever we need to. We also learned how to work with strings
so now we can deal with names, addresses etc. in contracts.
It is time to add these abilities to the contract program.
However, let's make the changes one at a time, focusing first
on allowing the list to grow.
The problem narrative remains almost exactly the same. The only
change is the removal of any reference to a limit on the number
of contracts in the list. It was the 'limit' data member in 'ListOfContracts' that represented
the maximum number of contracts so it should be removed. Since this property was part of 'ListOfContracts',
we can assume that there are no changes to the Contract class.
The data members 'limit' and 'numContracts' in the previous version of 'ListOfContracts' were special in that they were not properties that the users of the ListOfContracts class had any reason to know about. They were not like, for example, 'numDesks' - which was of interest to users. For that reason, there were no member functions to allow users of the class to retrieve or change the value of 'limit' or 'numContracts'. Notice also that these properties were never mentioned in the problem narrative. Rather, their existence was determined as part of the analysis of how to create a list structure.
In Section IV of this chapter you will study more such data members - data members
that exist because they make a data structure work. In this case, the data structure is a list and we now use it to meet the goal of having a collection of contracts that can grow (or shrink, although we will not worry about that here). As already noted, part of our task here is to keep track of how full the list is. To do that we need to keep track of two things - the maximum number of elements the list can hold at the present time and the actual number of elements in the list at present. These become data members!
Of course, the class declaration already has the data member 'numContracts'
to keep track of the actual number of elements on the list at
present. The class also had, up to a moment ago, a data member
to keep track of the maximum number of elements. It looks like
we need it back but let's change its name slightly to 'presentMax'
to emphasize that this maximum is not fixed in stone.
The earlier version also had an array to hold the elements. Our
new version will still have an array but this time the data member
will not be the array itself but a pointer to the array. This
way, when the program needs a bigger list, it can ask the operating
system for a bigger array and have the pointer point to this new
array. We will use the same name 'listOfContracts' to represent
this pointer.
Perhaps a set of algorithms for three key member functions will
make the processes involved clearer.
1. The Constructor:
This algorithm is similar to that for the constructor for the
previous version of this class except that we have to explicitly
ask for memory for the array since we have declared 'listOfContracts'
to be a pointer type.
2. The function member 'Add'
This function is again similar to the one found in the previous
version of the class except that it increases the size of the
array if needed instead of reporting a failure because the list
was full.
3. The function to increase the size of the list
Step five of this last algorithm introduces a new idea. Not only
can a program request memory from the system; it can also return
memory. This allows a program to make use of some memory for awhile
and later return it to the system, so that it can perhaps be used
in some other way later in the program. That is exactly what step
five does. The memory returned to the system may later become
part of a yet larger array of contracts.
Note that this last function uses pointers exactly as we used
them earlier in this chapter except that now the pointer variable
is pointing to a whole array. Pictures help make pointer manipulations
clear and you should use them in your own work. Here is a set
of figures demonstrating this function in action.
The first figure represents memory just before the algorithm starts and assumes that the list is full with three contracts (the value in 'presentMax'). Note that tempArray has been declared but contains garbage.

The second figure represents memory just after step three of the algorithm. At this point 'presentMax' has been incremented to six and 'tempArray' points to enough memory for this number of contracts.

In the next figure we see that the contracts that were in the array pointed to by 'listOfContracts' have been copied into the array pointed to by 'tempArray'. Note that there is still room in this new array.

The final figure represents memory after the last line of the algorithm. Now 'listOfContracts' also points to the new array. When the function that implements this algorithm is completed, the variable 'tempArray' will disappear since it is a local variable. Things will be back to the way they were before the function began except that 'presentMax' now holds a 6 instead of a 3 and 'listOfContracts' points to the larger array.

| |
Main Menu | Next Section |