< previous page page_1111 next page >

Page 1111
must state how the error will be handled if it occurs. Commonly, a function tests for the error condition and simply sets a flag to return if the error occurs, leaving the determination of what to do about the error to the calling code. The key point about error detection is that the interface between the calling code and a function must make it absolutely clear which party is responsible for the error checking.
Testing and Debugging Hints
1. Review the Testing and Debugging Hints for Chapter 17. They apply to the pointers and dynamic data that are used in dynamic linked lists.
2. Be sure that the link member in the last node of a dynamic linked list has been set to NULL.
3. When visiting the components in a dynamic linked list, be sure that you test for the end of the list in such a way that you don't try to dereference the null pointer. On many systems, dereferencing the null pointer causes a run-time error.
4. Be sure to initialize the external pointer to each dynamic data structure.
5. Do not use
currPtr++;
to make currPtr point to the next node in a dynamic linked list. The list nodes are not necessarily in consecutive memory locations on the free store.
6. Keep close track of pointers. Changing pointer values prematurely may cause problems when you try to get back to the pointed-to variable.
7. If a C++ class that points to dynamic data has a class destructor but not a copy-constructor, do not pass a class object to a function using pass-by-value. A shallow copy occurs, and both the formal parameter and the actual parameter point to the same dynamic data. When the function returns, the formal parameter's destructor is executed, destroying the actual parameter's dynamic data.
8. Be sure to test for possible error conditions when working with linked lists. There are two ways to handle error checking. The calling routine can check for the error condition and not call the function if the error occurs, or the function can test for the error condition. The documentation of the called function should state clearly how error checking is done.
Summary
Dynamic data structures grow and contract during run time. They are made up of nodes that contain two kinds of members: the component, and one or

 
< previous page page_1111 next page >