Exam Review 0
Question 0-1
Describe the purpose of the tombstone in the context of the
copy collection technique for garbage collection.
Question 0-2
Suppose we have the following sequence of declarations in C++.
class A { };
class B : public A { };
class C : public A { };
class D : public B, C { };
Note that the following would be legal in C++.
B *b = new D();
Why would C++ prohibit the following statement?
A *a = new D();
Question 0-3
C++ distinguishes regular inheritance from virtual
inheritance. Explain this distinction.
Question 0-4
How does a Smalltalk system store class methods internally?
Question 0-5
Explain why a subclass's CIR format should begin by matching the CIR
format of its superclass.
Solutions
Solution 0-1
In the course of copy collection, the garbage collector moves useful
memory allocations to other regions of memory.
Since other objects may still have pointers to the old location,
the system must somehow keep a placeholder, called a tombstone,
saying to where the allocation has moved.
Solution 0-2
This statement asks the C++ compiler to make a refer to the A
sub-object of a newly created D object.
Each D object has two A sub-objects - one by virtue of extending the B
class, another by virtue of extending the C class.
Because it is ambiguous which sub-object to select, C++ says that the
compiler should reject such a statement.
Solution 0-3
Normally, when a C++ class D has two parent classes that both
extend a common class A, each D object includes two
A sub-objects. This is regular inheritance.
With virtual inheritance, each D object would include
only one A sub-object.
Solution 0-4
When you define a class in Smalltalk, the system creates two objects:
a Class object A representing the class, and a Metaclass object
B.
Smalltalk remembers B as being the class of A.
Any class methods are stored in the method dictionary held by
B (and any instance methods are stored in the method dictionary
held by A). In this way, all methods are treated identically,
regardless of being an instance method or a class method: The method
goes to the class of the receiving object and looks the method up in the
method dictionary found there.
Solution 0-5
The code for an instance method is fixed to match the CIR format of the
class. A subclass can inherit the instance method, but the compiler will
not generate new code for the subclass. Thus, the code for this
inherited method is shared by the superclass and the subclass. Since
this generated code will expect instance variables to be located in
the CIR as in the superclass's CIR format,
the subclass's CIR format should begin by matching this format
in order for subclass instances be compatible with this existing code.