Docsity

Memory Layout and Snapshot of C++ and ML Classes, Assignments of Programming Languages

Toggle sidebar
bg1
Homework 8
due before class Dec 3, 2008
1. Suppose we have the following C++ code.
#include <stdio.h>
typedef enum {TYPE_INT, TYPE_FLOAT} ValType;
class AST
{
public:
virtual ValType get_val_type() = 0;
virtual void print() = 0;
};
class Var : public AST
{
private:
const char* name;
ValType varType; // the declared type of variable
public:
Var(const char* _name, ValType _type) : name(_name), varType(_type) {}
const char* get_name() { return name; }
virtual ValType get_val_type() { return varType; }
virtual void print()
{
if (varType == TYPE_INT) printf("%s : int\n", name);
else printf("%s : float\n", name);
}
};
class Plus : public AST
{
private:
AST *opd1, *opd2;
public:
Plus(AST* _opd1, AST* _opd2) : opd1(_opd1), opd2(_opd2) {}
AST* get_opd1() { return opd1; }
AST* get_opd2() { return opd2; }
ValType get_type()
{
if (opd1->get_val_type() == TYPE_FLOAT ||
1
pf3
pf4

Partial preview of the text

Download Memory Layout and Snapshot of C++ and ML Classes and more Assignments Programming Languages in PDF only on Docsity!

Homework 8

due before class Dec 3, 2008

  1. Suppose we have the following C++ code.

#include <stdio.h> typedef enum {TYPE_INT, TYPE_FLOAT} ValType; class AST { public: virtual ValType get_val_type() = 0; virtual void print() = 0; };

class Var : public AST { private: const char* name; ValType varType; // the declared type of variable public: Var(const char* _name, ValType _type) : name(_name), varType(_type) {} const char* get_name() { return name; } virtual ValType get_val_type() { return varType; } virtual void print() { if (varType == TYPE_INT) printf("%s : int\n", name); else printf("%s : float\n", name); } };

class Plus : public AST { private: AST opd1, opd2; public: Plus(AST _opd1, AST _opd2) : opd1(_opd1), opd2(_opd2) {} AST* get_opd1() { return opd1; } AST* get_opd2() { return opd2; } ValType get_type() { if (opd1->get_val_type() == TYPE_FLOAT ||

opd2->get_val_type() == TYPE_FLOAT) return TYPE_FLOAT; return TYPE_INT; } virtual ValType get_val_type() { return get_type(); } virtual void print() { opd1->print(); printf("+\n"); opd2->print(); } };

int main() { AST* res = new Plus( new Var("x1",TYPE_INT), new Var("y1", TYPE_FLOAT)); res->print(); }

(a) Draw a picture of the memory layout for the classes AST, Var, and Plus.

AST: vptr -> vtable: get_val_type -> impl of AST:get_val_type print->impl of AST:print

Var: vptr -> vtable: get_val_type -> impl of Var:get_val_type print->impl of Var:print name ->? varType :?

Plus: vptr -> vtable: get_val_type -> impl of Plus:get_val_type print -> impl of Plus:print opd1->? opd2->?

(b) Draw a picture of the layout of the object res allocated within the main function.

res -> vptr -> vtable: get_val_type -> impl of Plus:get_val_type print -> impl of Plus:print opd1 -> vptr -> vtable: get_val_type -> impl of Var:get_val_type print->impl of Var:print name -> "x1" varType : TYPE_INT opd2 -> vptr -> vtable: get_val_type -> impl of Var:get_val_type print->impl of Var:print name -> "y1" varType : TYPE_FLOAT

(c) The following is an alternative implementation of the above C++ classes in ML using function closures. datatype ValType = TYPE_INT | TYPE_FLOAT fun mk_Var(name,vartype) =

vartype : TYPE_INT var_print -> Closure(mk_Var_x1, code of var_print) var_get_val_type -> Closure(mk_Var_x1, code of var_get_val_type)

mk_Var_y1: Control Link -> global Access Link -> global Return address -> after call to mk_Var("y1",TYPE_FLOAT) Return Result Address -> global.res_mk_Var_y name: "y1" vartype : TYPE_FLOAT var_print -> Closure(mk_Var_y1, code of var_print) var_get_val_type -> Closure(mk_Var_y1, code of var_get_val_type)

mk_Plus: Control Link -> global Access Link -> global Return address -> after call to mk_Plus(...) in global Return Result Address -> global.res opd1 -> StorageOf(global.res_mk_Var_x1) opd2 -> StorageOf(global.res_mk_Var_y1) plus_print -> Closure(mk_Plus, code of plus_print) plus_get_val_type -> Closure(mk_Plus, code of plus_get_val_type)