【正文】
inters. ? 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。Compiler Theory Fall 2021 Jianhui Yue Chapter 7 Dynamic Memory Parameter Passing Mechanisms Instructor Jianhui Yue Software College SCU Compiler Theory Fall 2021 Jianhui Yue Limitations of StackBased Environment ? If a reference to a local variable in a procedure can be returned to the caller, the result will be dangling reference. – When the procedure is exited, the activation record will be deallocated from the stack. ? addr = dangle() causes addr to point to an unsafe location. ? Such program is erroneous in C. – (No piler will give an error message) int *dangle() {int x。 void f()。 void f()。 unsigned nunits。 ? newp =p。 ? memptr=newp。 ? /* search the list to find the block containing ap */ ? prev+=p+p。 ++x。y)。} /* correct */ inc2(y)。 p(a,a)。 ++x。 return 0。 a[1]=1。} a has value of 3 after p is called if pass by reference is used. a has value of 2 after p is called if pass by valueresult is used. Compiler Theory Fall 2021 Jianhui Yue Pass by Name ? The idea is that the argument is not evaluated until its actual use in the called program. ? The name of the argument replaces the name of the parameter it corresponds to. int i。 ++y。 x) { ++x。 ++x。 ? memptr=prev。