oo language requires special mechanisms in the runtime environment to implement their added features Objects, methods, inheritance, and dynamic binding oo languages vary greatly in their requirements for the runtime environment Two good representatives of the extreme Smalltake requires fully dynamic environment C++ retains the stack-based environment ofc in much of its design efforts
• OO language requires special mechanisms in the runtime environment to implement their added features: – Objects, methods, inheritance, and dynamic binding • OO languages vary greatly in their requirements for the runtime environment. • Two good representatives of the extreme: – Smalltake requires fully dynamic environment; – C++ retains the stack-based environment of C in much of its design efforts
Object in memory can be viewed as a cross between a traditional record structure and an activation record One mechanism would be for initialization code to copy all the currently inherited features(and methods) directly into the record structure(with methods as code pointer) An alternative is to keep a complete description of the class structure in memory at each point during execution, with inheritance maintained by superclass pointers, and ll method pointers kept as field in the class structure (Inheritance graph) Another alternative is to compute the list of code pointers for available methods of each class, and store this in(static) memory as a virtual function table. It is the method of choice in c++
• Object in memory can be viewed as a cross between a traditional record structure and an activation record • One mechanism would be for initialization code to copy all the currently inherited features (and methods) directly into the record structure (with methods as code pointer); • An alternative is to keep a complete description of the class structure in memory at each point during execution, with inheritance maintained by superclass pointers, and all method pointers kept as field in the class structure (Inheritance graph). • Another alternative is to compute the list of code pointers for available methods of each class, and store this in (static) memory as a virtual function table. It is the method of choice in C++
Example: Consider the following C++ class declarations Class a dpublic double x,y void f0; virtual void go class B: public a dpublic double z: void fO virtual void ho
Example: Consider the following C++ class declarations: Class A {public: double x, y; void f(); virtual void g(); } class B : public A {public: double z; void f(); virtual void h(); }
An object of class A would appear in memory as follows Virtual function g Virtual function table for A Table pointer While the object of class b would appear as follows Virtual function Virtual function table for B table pointer B: h Note: The virtual function pointer, once added to the object structure, remains in a fixed location. The f does not obey dynamic binding in C++, since it is not declared virtual
An object of class A would appear in memory as follows: x y Virtual function Table pointer While the object of class B would appear as follows: x y Virtual function table pointer z Note: The virtual function pointer, once added to the object structure, remains in a fixed location. The f does not obey dynamic binding in C++, since it is not declared “virtual”. A::g Virtual function table for A A::g Virtual function table for B B::h
7.4.3 Heap Management
7.4.3 Heap Management