This does not copy the string; it copies the pointer to a string. That is, after sailor is initialized to sports, you wind up with two pointers to the same string. That’s not a problem when the operator<<() function uses the pointer to display the string. It is a problem when the destructor is called. Recall that the StringBad destructor frees the memory pointed to by the str pointer. The effect of destroying sailor is this:

delete [] sailor.str;    // delete the string that ditto.str points to

The sailor.str pointer points to "Spinach Leaves Bowl for Dollars" because it is assigned the value of sports.str, which points to that string. So the delete statement frees the memory occupied by the string "Spinach Leaves Bowl for Dollars".

Next, the effect of destroying sports is this:

delete [] sports.str;   // effect is undefined

Here, sports.str points to the same memory location that has already been freed by the destructor for sailor, and this results in undefined, possibly harmful, behavior. In the case of Listing 12.3, the program produces mangled strings, which is usually a sign of memory mismanagement.

Another disturbing symptom is that attempting to delete the same memory twice can cause the program to abort. Microsoft Visual C++ 2010 (debug mode), for example, displays an error message window saying “Debug Assertion Failed!”, and g++ 4.4.1 on Linux reports “double free or corruption” and aborts. Other systems might provide different messages or even no message, but the same evil lurks within the programs.

Fixing the Problem by Defining an Explicit Copy Constructor

The cure for the problems in the class design is to make a deep copy. That is, rather than just copy the address of the string, the copy constructor should duplicate the string and assign the address of the duplicate to the str member. That way, each object gets its own string rather than referring to another object’s string. And each call of the destructor frees a different string rather than making duplicate attempts at freeing the same string. Here’s how you can code the String copy constructor:

StringBad::StringBad(const StringBad & st)

{

    num_strings++;             // handle static member update

    len = st.len;              // same length

    str = new char [len + 1];  // allot space

    std::strcpy(str, st.str);  // copy string to new location

    cout << num_strings << ": \"" << str

         << "\" object created\n"; // For Your Information

}

What makes defining the copy constructor necessary is the fact that some class members are new-initialized pointers to data rather than the data themselves. Figure 12.3 illustrates deep copying.

Figure 12.3. An inside look at deep copying.

Caution

If a class contains members that are pointers initialized by new, you should define a copy constructor that copies the pointed-to data instead of copying the pointers themselves. This is termed deep copying. The alternative form of copying (memberwise, or shallow, copying) just copies pointer values. A shallow copy is just that—the shallow “scraping off” of pointer information for copying, rather than the deeper “mining” required to copy the constructs referred to by the pointers.

More Stringbad Problems: Assignment Operators

Not all the problems in Listing 12.3 can be blamed on the default copy constructor; you have to look at the default assignment operator, too. Just as ANSI C allows structure assignment, C++ allows class object assignment. It does so by automatically overloading an assignment operator for a class. This operator has the following prototype:

Class_name & Class_name::operator=(const Class_name &);

That is, it takes and returns a reference to an object of the class. For example, here’s the prototype for the StringBad class:

StringBad & StringBad::operator=(const StringBad &);

When an Assignment Operator Is Used and What It Does

An overloaded assignment operator is used when you assign one object to another existing object:

StringBad headline1("Celery Stalks at Midnight");

...

StringBad knot;

knot = headline1;   // assignment operator invoked

An assignment operator is not necessarily used when initializing an object:

StringBad metoo = knot; // use copy constructor, possibly assignment, too

Here metoo is a newly created object being initialized to knot’s values; hence, the copy constructor is used. However, as mentioned before, implementations have the option of handling this statement in two steps: using the copy constructor to create a temporary object and then using assignment to copy the values to the new object. That is, initialization always invokes a copy constructor, and forms using the = operator may also invoke an assignment operator.

Перейти на страницу:

Все книги серии Developer's Library

Похожие книги