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 programto crash or causes a segmentation fault. Segmentation fault are usually reported afterthe program terminates. The moral is that if you have any of destructor, copy constructor or operator = you likely need all three.
You might also like to view...
If you run a query and a dialog box appears asking you to enter data, it is a ________
Fill in the blank(s) with correct word
If you need to find the day of the month on which events occurred from a list of dates in the named range of Start_Date, which formula would provide the correct result?
A) =IF(DAYMONTH(Start_Date)<31,Start_Date,1 ) B) =DAY(Start_Date) C) =DAYMONTH(Start_Date) D) =DAYS(End_Date,Start_Date)