< previous page page_1030 next page >

Page 1030
        cout << ** In CopyFrom function, not enough memory
             <<  for  << size <<  elements ** << endl;
        exit(1);
    }
    for (i = 0; i < size; i++)
         arr[i] = array2.arr[i];
}
Testing and Debugging
Programs that use pointers are more difficult to write and debug than programs without pointers. Indirect addressing never seems quite as normal as direct addressing when you want to get at the contents of a variable.
The most common errors associated with the use of pointer variables are
1. confusing the pointer variable with the variable it points to
2. trying to dereference the null pointer or an uninitialized pointer
3. inaccessible objects
4. dangling pointers
Let's look at each of these in turn.
If ptr is a pointer variable, care must be taken not to confuse the expressions ptr and *ptr. The expression
ptr
accesses the variable ptr (which contains a memory address). The expression
*ptr
accesses the variable that ptr points to.
ptr1 = ptr2
Copies the contents of ptr2 into ptr1.
*ptr1 = *ptr2
Copies the contents of the variable pointed to by ptr2 into the variable pointed to by ptr1.
*ptr1 = ptr2
Illegalone is a pointer and one is a variable being pointed to.
ptr1 = *ptr2
Illegalone is a pointer and one is a variable being pointed to.

 
< previous page page_1030 next page >