It is an error to call the delete operator on a pointer a second time. Consider what happens if there is memory allocated on the free store for a class, a destructor, and initializing constructors have been defined, but no copy constructor was defined. Now suppose a function is called that has call-by-value parameters. Why does the program either die in the middle or give a segmentation fault?
What will be an ideal response?
The call-by-value parameters cause the default (compiler generated) copy constructor to be called. The default copy constructor copies the pointer members of the class. This makes the value parameters class object’s pointers point to the argument’s data. When the function terminates, the destructor is called on the parameter. The destructor calls delete on the pointers in parameter. Unfortunately, this data is the same data that the argument’s pointers point to, so the argument’s pointers are now dangling. When the argument goes out of scope, the destructor is called. This calls delete on pointer variables in the argument object. The result it that these pointer have been deleted twice, once at function termination and once when the argument goes out of scope. The result is a segmentation fault, which either causes the program to crash or causes a segmentation fault. Segmentation fault are usually reported after the program terminates. The moral is that if you have any of destructor, copy constructor or operator = you likely need all three. (This actually occurred to this writer while writing another textbook.)
You might also like to view...
Each module starts with a ____ section, which contains statements that apply to all procedures in the module.
A. Standard B. Declarations C. Class D. Local
Storage areas declared for a class are referred to as ___________________.
Fill in the blank(s) with the appropriate word(s).
For two benchmarks, x and y, show that their arithmetic mean is always higher than, or the same as, the geometric mean.
What will be an ideal response?
For security reasons, you should use the ____________________ property when possible instead of the innerHTML property.?
Fill in the blank(s) with the appropriate word(s).