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.

Computer Science & Information Technology

You might also like to view...

Java programs are stored in zip files called class files, which have the file extension .class.

Answer the following statement true (T) or false (F)

Computer Science & Information Technology

The ____ selector specifies a series of nested elements and selects the final, most deeply nested element.

A. direct child B. next sibling C. direct next sibling D. descendant

Computer Science & Information Technology

It is possible to minimize response time by running only interactive jobs and letting batch jobs wait until the interactive load ceases.

Answer the following statement true (T) or false (F)

Computer Science & Information Technology

Classes and methods reveal low-level implementation details.

Answer the following statement true (T) or false (F)

Computer Science & Information Technology