【正文】
nce – Dynamic binding ? OO languages vary in their requirements for the runtime environment: – C++ retains the stackbased environment of C, no automatic dynamic memory management – Smalltalk requires fully dynamic environment similar to LISP Compiler Theory Fall 2021 Jianhui Yue Implementation of Objects ? Keep a plete description of the class structure in memory during execution. ? Maintain the inheritance by the superclass pointers. ? Keep pointers to all methods. ? Each object keeps: – Fields for instance variables – A pointer to its defining class ? All methods (local and inherited) are found through it. ? Instance variables have predictable offsets. ? Methods do not. They are maintained in a symbol table structure with lookup capabilities. Compiler Theory Fall 2021 Jianhui Yue Implementation of Objects (cont) ? An alternative approach is to pute a list of code pointers for methods of each class, and store this in (static) memory as a virtual function table. ? It can be arranged so that each method has a predictable offset. ? Each object contains a pointer to the appropriate virtual function table. ? It is the method of choice for C++. Compiler Theory Fall 2021 Jianhui Yue Example class A { public: double x, y。 void f()。 virtual void g()。 }。 class B: public A { public: double z。 void f()。 virtual void h()。 }。 x y Virtual function table pointer A::g x y Virtual function table pointer z A::g B::h Object A Object B Compiler Theory Fall 2021 Jianhui Yue Heap ? The data structure that handles pointer allocation and deallocation is called heap. ? Heap is usually allocated as a linear block of memory in a way that it can grow, while interfering as little as possible with the stack. ? Heap provides to operation 1. Allocate – Takes size parameter – Return a pointer to a block of memory of the correct size or null if none exists. 2. Free – Takes a pointer to an allocated block of memory – Marks it as being free – Must be able to find the size of the block. Compiler Th