Memory Allocation -stack and Heap Based
Generally, programming languages use two different techniques for allocation of memory
Stack-based allocation: space
requirement determined at compile time based on static types
-memory alocation/release tied to procedure entry/exit
-performed very efficiently
Heap-based allocation: space
requirement determined at runtime based upon dynamic considerations
- memory allocation/release not tied to procedure
entry/exit
- handled by user or by run-time library(garbage
collection)
- consideration somewhat less efficient
class window{
public:
virtual void
oops( );
private:
int
height;
int
width;
};
class TextWindow : public Window {
public:
virtual void
oops( );
private:
char * contents;
int cursorLocation;
};
Window x;
TextWindow y;
Memory Strategies
How much memory should be set aside for the variable x?
Minimum Static Space Allocation:allocate
space necessary for base class only
Maximum Static space Allocation: allocate
space necessary for largest subclass
Dynamic space Allocation: allocate only
space necessary for pointer
Minimum Static Space Allocation
The Slicing Problem
problem: trying to take a large box and squeeze it into
a small space
clearly this won't work.
thus, the extra fields are simply sliced off (i.e., a
type conversion)
Question: Does it matter?
Answer: Only if somebody notices
Solution: Design the language to make it difficult
to notice
Rules for Member Function Binding
Deciding what member function to execute complicated by slicing
Variables declared normally:binding of member function name to function body based on static type (regardless whether the function virtual)
Variables declared as references or pointers:binding
of member function name to function body based on dynamic type if function
virtual,on static type otherwise
void Window::oops( )
{
printf("window oops\n");
void TextWindow::oops( )
{
printf("TextWindow oops %d\n",cursorlocation);
}
TextWindow x;
Window a;
Window *b;
TextWindow * c;
a=x; a.oops( ); //execute
Window version
b=&x; b.oops( ); // executes TextWindow or
Window version
c= &x; c.oops( ); // executes TextWindow version
Suppose a value is indirectly accessed through a pointer
When an assignment is performed(or , equality test is
made), is the quantity assigned (compared) simply the pointer or is it
actual value?
Problems with Pointer Semantics
Equality testing is one place where it might useful to be able to change the type of method arguments
class Shape
{ . . . .
public boolean equals(shape
s) { return false;}
}
class Triangle extends Shape
{ . . .
public boolean equals(Triangle
t) { . . . }
}
class Square extends Shape
{ . . .
public boolean equals(Square
sq) { . . . }
}
class Shape
{ . . . .
public boolean equals(shape
s) { return false;}
}
class Triangle extends Shape
{ . . .
public boolean equals(Triangle
t) {
if (s instanceof
Shape )
{ Triangle t=(Triangle) s;
// do comparison and return result
}
else
return false;
}
}
class Square extends Shape
{ . . .
public boolean equals(Square
sq)
{ Triangle t;
try{t=(Triangle) s;} catch (ClassCastException e) { return false }
//
do comparison and result result
}
}