procedureAbstraction

procedureAbstraction - Procedure and ObjectOriented...

Info iconThis preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon
cs5363 1 Procedure and Object- Oriented Abstraction Scope and storage management
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
cs5363 2 Procedure abstractions Procedures are fundamental programming abstractions They are used to support dynamically nested blocks Paired function call and return jumps They have standalone semantics defined by an abstraction interface input parameters, return values, global side effects Procedures are units of separate compilation They represent parameterized blocks of computation int main(int argc, char* argv[]) { float a; a = foo(…) } float foo(…) { int a, b, c; …. r = bar(…); return r; } float bar(…) { float r; …. return r; }
Background image of page 2
cs5363 3 Scoping rules Global and local variables Static scoping Find global variables in enclosing blocks in program text Dynamic scoping Find global variables in the most recently evaluated blocks Easier to implement in interpreted languages What is the scoping rule for C/C++, Java? program main(input,output); var x : integer; function g(z: integer) :integer; begin g := x+z end; function h(z: integer) :integer; var x : integer; begin x := 1; h:=g(z) end; begin x := 0; print(h(3)) end x 0 x 1 z 3 z 3 outer block h(3) g(12)
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
cs5363 4 Simplified memory model Activation record pointer(rarp) Program Counter Data Code Heap/ static data Stack …… Runtime stack: activation records of blocks/functions Block entry: add new data to stack Block exit: remove outdated data Heap: data of varying lifetime Variables that last throughout the program Address may be contained by variables on the runtime stack
Background image of page 4
cs5363 5 Managing Data Storage Local variables --- activation records on stack Declared inside a block (e.g. function body) Enter block: allocate space Exit block: de-allocate space Local variables in an enclosing block Already allocated before entering current Block Remain allocated after exiting current block Function parameters and return value Allocated and initialized before entering function body Formal parameters dallocated after exiting function body Global/static variables --- static data areas Allocated when program is loaded to memory Storage remain until program exits Dynamically allocated variables --- heap Storage dynamically allocated at runtime (e.g., malloc in C) Storage remain until explicitly de-allocated or garbage collected
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
cs5363 6 Activation Record Allocate storage for each block dynamically Allocate an activation record before evaluating each block Storage for each local variable determined as compile time Values of local variables evaluated at runtime Delete the activation record after block exits May need space for intermediate results such as (x+y), (x-y ) { int x=0; int y=x+1; { int z=(x+y)*(x-y); }; }; Allocate AR with space for x, y Set values of x, y Allocate AR for inner block Set value of z Delete AR for inner block Delete AR for outer block
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 27

procedureAbstraction - Procedure and ObjectOriented...

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online