Lectures 3-4-5-6
Basics in Procedural Programming: Machinery
prof. Marco Bellia, Dip. Informatica, Università di Pisa
February 21-28, 2014
prof. Marco Bellia, Dip. Informatica, Università di Pisa
2014-03-01 11:04:07
Lectures 3-4-5-6 Basics in Procedural Programming: Machinery
1/48
Lecture3-6E-2014.pdf (#21)
Basics in Procedural Programming: Machinery
Naming and Binding
Mutable Values: Denotable, Storable and Expressible Value
Env, Store, AR and Blocks: Motivations
Blocks: Inline blocks and Procedure/function (body) block
Blocks: Static and Dynamic Scope
Activation Records: Structure and Implementation
Programming Unit
Aliasing, Closures, Lambda Lifting
Env: Formalization and Implementation
Store: Formalization and Implementation
prof. Marco Bellia, Dip. Informatica, Università di Pisa
2014-03-01 11:04:08
Lectures 3-4-5-6 Basics in Procedural Programming: Machinery
2/48
Lecture3-6E-2014.pdf (2/48)
Naming and Binding
Naming = Use of identifiers to refer to definitions of
programming entities
Definition of the entity = Definition results a Denotable
Value of the language semantic domain Den
Example
final double pigreco = 3.15; /*an example of constant*/
int y = 5; /*an example of variable*/
See next slide, for other examples of definitions.
Binding = Association between the name and its definition
Bindings of a program are all collected in the semantic
structure Env
prof. Marco Bellia, Dip. Informatica, Università di Pisa
2014-03-01 11:04:08
Lectures 3-4-5-6 Basics in Procedural Programming: Machinery
3/48
Lecture3-6E-2014.pdf (3/48)
Mutable and Immutable values
Names for Constants and Variables are in common use in the
introduction (i.e. declaration) of values
Variable = It is synonym of Mutable Value
Mutable values are basics in Imperative Languages but they are non
incompatible with Descriptive Languages, in principle
However, problems arise from the different ways in which such values can
be used in the program: Haskell preserves Transparency Property, instead
Ocaml does not.
Constant = It is an example of Immutable Value
Example
Var x: int /*Pascal Declaration of a mutable value*/
Const y:int /*Pascal Declaration of a immutable value*/
int z[] /*C Declaration of an immutable, structured, value with mutable components of type int */
*int y /*C Declaration of an mutable value that it is, in turn, a mutable value yet, namely a pointer*/
label u /*Pascal Declaration of a immutable value, namely a position in program */
void p(...){...} /*Declaration of an immutable value, namely a procedure*/
public class A{...} /*Declaration of an immutable value, namely a class (of Java)*/
struct S{...} /* Declaration of an immutable value, namely a type record (of C) */
type B = ... /*Again, declaration, in OCaml, of an immutable value, namely a (concrete) type*/
prof. Marco Bellia, Dip. Informatica, Università di Pisa
2014-03-01 11:04:08
Lectures 3-4-5-6 Basics in Procedural Programming: Machinery
4/48
Lecture3-6E-2014.pdf (4/48)
Mutable and Immutable values: The Equality Property
The two classes of values definitely, differ in some form of state that is underlying of
mutable values. This is clearly, reflected from the behavior of (almost all) operations
of the two classes, and then, from their use in programming.
However, what about comparing two values?
The Equality Property = Two values are equals only if they
can be exchanged with one another, in the program.
Each mutable value is equal only to itself
Example
Comment the equality predications in the following C++ text:
prof. Marco Bellia, Dip. Informatica, Università di Pisa
2014-03-01 11:04:08
Lectures 3-4-5-6 Basics in Procedural Programming: Machinery
5/48
Lecture3-6E-2014.pdf (5/48)
Mutable and Immutable values: Implementation Skills
The two classes of values definitely, differ in some form of state that is underlying of
mutable values. This is clearly, reflected from the behavior of (almost all) operations
of the two classes, and then, from their use in programming.
However, what can we say about the internal representation of
such values?
Implementation Skills
Example
One immuable and one immutable value in two different memory organizations:
Memory on the right has a constant pool memory.
prof. Marco Bellia, Dip. Informatica, Università di Pisa
2014-03-01 11:04:08
Lectures 3-4-5-6 Basics in Procedural Programming: Machinery
6/48
Lecture3-6E-2014.pdf (6/48)
Den, Mem, Val: The Value Domains of a Language
The Value (semantic) Domains are a fundamental
characteristic of a language
They highly constrain the way in which algorithms may be
written, in the language
Val = Domain of the Values that can be involved in the
language programs
Den = D. of the Values that can be expressed in definitions
Mem = D. of the Mutable Values of the language
The following hold: (1) Den ⊆ Val; (2) Mem ⊆ Val
But: Den ∪ Mem = Val; Mem ⊆ Val ; and so on ... hold or
not depending on the language
Example
int A[3] = {3,5,17}; /* define, in C, a mutable value in a binding for A */
A = {3,5,12}; /* is not permitted */
Then: Is {3,5,17} defining a mutable or immutable value?
In providing for an answer, compare it with:
int B = 3; /* define, in C, a mutable value in a binding for B */
B = 15; /* a common statement in C */
Then: 3 and 15 are immutable integers.
What can you say about the other languages that you know?
prof. Marco Bellia, Dip. Informatica, Università di Pisa
2014-03-01 11:04:08
Lectures 3-4-5-6 Basics in Procedural Programming: Machinery
7/48
Lecture3-6E-2014.pdf (7/48)
Expressible Values
Expressible Values = Have an explicit, syntactic, presentation
in the language;
These values are useful for introducing constant values in
expressions;
Hence, values of common use in the expressions of the
language are also expressible value;
Example
Are
Are
Are
Are
Are
Integers are expressible values of C
arrays expressible values of C?
lists expressible values of Ocaml (Haskell)?
vectors expressible values of Java?
functions expressible values of Ocaml (Haskell)?
methods expressible values of Java?
prof. Marco Bellia, Dip. Informatica, Università di Pisa
2014-03-01 11:04:08
Lectures 3-4-5-6 Basics in Procedural Programming: Machinery
8/48
Lecture3-6E-2014.pdf (8/48)
Env, Store, AR
Env = Semantic Structure for collecting the bindings of the
program
It is quite related to the symbol tables of the front-end of
language executors and Compilers
Machine Languages do not use naming and do not require Env
Store = Semantic Structure for Mutable Values, i.e. Mem
Additional Memory components are always present in the
implementation machinery, to handle immutable values and
the program representation of all languages (including pure
Functional ones)
AR = Implementation Machinery Component for the
computation control
It is used to support the program sectioning into parts that:
can be executed separately,
and, include inline blocks, procedures/functions, modules,
monitors, threads,..
prof. Marco Bellia, Dip. Informatica, Università di Pisa
2014-03-01 11:04:08
Lectures 3-4-5-6 Basics in Procedural Programming: Machinery
9/48
Lecture3-6E-2014.pdf (9/48)
Program Sectioning: Blocks
Block = It is used for creating sections of program that are:
(partially) autonomous in the definitions that may be used, and
may exhibit specific functionalities, and
may be valid supports in program verification and modification
Two main kinds in Procedural Programming:
inline blocks
procedures and functions
prof. Marco Bellia, Dip. Informatica, Università di Pisa
2014-03-01 11:04:08
Lectures 3-4-5-6 Basics in Procedural Programming: Machinery
10/48
Lecture3-6E-2014.pdf (10/48)
Blocks: Inline vs. Procedures
Inline Blocks
anonymous
contain two parts: Local Definitions and End/Exit Code;
may be nested: Execution exits nested blocks in reverse order
to the entering
Procedures and Functions
named
contain three parts: Parameter Transmission, Local Definitions
and Return/Exit Code;
Example
(a) According to the above features, describe the features of the blocks of the compound statements of the
language C.
(b) Moreover, answer to: in what features the inline blocks of Java differ from the ones described in the slide
Suggested Reading:
Gabrielli M., S. Martini, Programming Languages: Principles and Paradigms, Springer, 2006 - Chapter 4-4.2
prof. Marco Bellia, Dip. Informatica, Università di Pisa
2014-03-01 11:04:08
Lectures 3-4-5-6 Basics in Procedural Programming: Machinery
11/48
Lecture3-6E-2014.pdf (11/48)
Blocks: Inline vs. Procedures - Exercises
Example
(a) According to the above features, describe the features of the blocks of the compound statements of C.
(b) Moreover, answer to: in what features the inline blocks of Java differ from the ones described in the slide
(a) Answer.
• anonymous;
• contains two parts:
1. Local Definitions: But without procedure/functions
2. Code: Any sequence of statements including jump stms. (break, return, continue, goto)
• may be nested: Execution exits depend on the Code stms.
(b) Answer.
• anonymous;
• contains two parts:
1. Local Definitions (including classes, hence methods)
2. Code: Any sequence of statements including jump stms. (break, return, continue, goto)
• may be nested: Execution exits depend on the Code stms.
prof. Marco Bellia, Dip. Informatica, Università di Pisa
2014-03-01 11:04:08
Lectures 3-4-5-6 Basics in Procedural Programming: Machinery
12/48
Lecture3-6E-2014.pdf (12/48)
Blocks: Scope of Identifier definitions
Scope. Let I be an identifier defined with the value d in a
block A, of a program P, i.e. binding(A,I)=d in P. Then,
Scope(I,A) is the set Z of sections of P that must use the
value d when they refer to the identifier I:
Scope(I,A)={B | binding(B,I)=binding(A,I)}
Definition of Scope depends from the language;
Two kinds of Scope (and correspondingly, two classes of
languages):
Scope is static (Hence, Languages with static Scope)
Scope is dynamic (Hence, Languages with dynamic Scope)
prof. Marco Bellia, Dip. Informatica, Università di Pisa
2014-03-01 11:04:08
Lectures 3-4-5-6 Basics in Procedural Programming: Machinery
13/48
Lecture3-6E-2014.pdf (13/48)
Blocks: Static and Dynamic Scope
Scope(I,A)={B | binding(B,I)=binding(A,I)}
Static Scope: S-Scope
Z includes A;
Z includes also, any block B which is:
(defined) within A and
it is such that its section ’Local Definitions’ does not contain
a new definition for I
in this case, I is also, called a non-local of B.
Dynamic Scope: D-Scope
Z includes A;
Z includes also, any block B which is:
executed during the execution of the ’Code’ of A and
it is such that its section ’Local Definitions’ does not contain
a new definition for I
in this case, I is also called a non-local of B.
prof. Marco Bellia, Dip. Informatica, Università di Pisa
2014-03-01 11:04:09
Lectures 3-4-5-6 Basics in Procedural Programming: Machinery
14/48
Lecture3-6E-2014.pdf (14/48)
Blocks: Static and Dynamic Scope/2
They differ only on the non-locals of procedures and functions
Example
Give names to inline blocks by using capital letters, in alphabetic order, from A that is assigned to the
outermost, topmost, block;
1 List the block in the program;
2 Compute the function Scope of each defined identifiers;
3 Compute the static, S-Scope, and dynamic, D-Scope, scope of each defined identifiers;
4 Show printed values when static, respectively dynamic, scope is used
A:{int x = 0;
void pippo(int n){x=n+x;}
pippo(3);
print(x);
printer: 3 3
B:{int x = 0;
pippo(3);
print(x);
printer: 0 3
}
print(x);
printer: 6 3
}
(1) The program blocks are: {A,pippo, B};
(2) Scope(A,x)={A,pippo}; Scope(B,x)={B,pippo}; Scope(pippo,n)={pippo}
(3) S-Scope(A,x)={A,pippo}; S-Scope(B,x)={B}; S-Scope(pippo,n)={pippo}
D-Scope(A,x)={A,pippo}; D-Scope(B,x)={B,pippo}; D-Scope(pippo,n)={pippo}
prof. Marco Bellia, Dip. Informatica, Università di Pisa
2014-03-01 11:04:09
Lectures 3-4-5-6 Basics in Procedural Programming: Machinery
15/48
Lecture3-6E-2014.pdf (15/48)
Static vs. Dynamic Scope: Motivations
Two kinds of Scope (and correspondingly, two classes of
languages):
Scope is static (Almost all languages)
Also called, lexical scope (Symbol-Tables of front-ends)
The binding of a non-local is localized near to its use
The binding of a non-local in a block is the same in all block
executions (during each program execution)
Allow a better sectioning of the program;
Allow a better programming approach (programming
methodologies)
Implementation is efficient but a bit heavy.
Scope is dynamic (Lisp-like languages)
Avoid the use of non-locals is recommended in the use of
languages with dynamic scope (lambda-lifting).
Implementation is not efficient but very easy to do.
prof. Marco Bellia, Dip. Informatica, Università di Pisa
2014-03-01 11:04:09
Lectures 3-4-5-6 Basics in Procedural Programming: Machinery
16/48
Lecture3-6E-2014.pdf (16/48)
Blocks: Different Notions
In some languages (including Java) inline blocks cannot re-define a non-local variable (i.e.the shadowing of
local variables is forbidden)
In some languages blocks are not always, enclosed by delimiters (non ANSI C), or declarations may occur
everywhere in a block (JavaScripts)
Example
{int x = 5;
...
{int y = 0;
x+1;
...
int x = 10; This declaration may be considered:
y = x+y;
(a) either, the beginning of a new block, ending at the end of its outer block (non ANSI C)
}
(b) or, to be moved to the beginning of the block in which it is declared (JavaScript).
...
}
How many blocks here?
Example
{int x = 4;
while(x > 0){
- -x;
int x;
print(x);
}
...}
What is while supposed to compute according to the two readings, (a) and (b) above?
{int x = 4;
while(x > 0){
int x;
- -x;
print(x);
}
...}
Provide a re-phrasing in ANSI C of the code and
show the first 10 printed rows and comment them.
prof. Marco Bellia, Dip. Informatica, Università di Pisa
2014-03-01 11:04:09
Lectures 3-4-5-6 Basics in Procedural Programming: Machinery
17/48
Lecture3-6E-2014.pdf (17/48)
Activation Record: Implementation for inline blocks
Activation Records:
Support the execution of the code of a block (i.e. program
section)
Support the control transfer among different blocks
Have different structure depending on:
inline block:
Env (called frame)
Program Counter (pc)
Memory Section for Expression Intermediate Results (ri)
Dynamic Chain pointer (cd)
Example
prof. Marco Bellia, Dip. Informatica, Università di Pisa
2014-03-01 11:04:09
Lectures 3-4-5-6 Basics in Procedural Programming: Machinery
18/48
Lecture3-6E-2014.pdf (18/48)
Activation Record: Implementation for procedure blocks
Activation Records:
inline block: ...
procedure block:
Env (called frame)
Program Counter (pc)
Memory Section for Expression Intermediate Results (ri)
Dynamic Chain pointer (cd)
Static Chain pointer (cs) only for static scope
Return Address (ret)
Result Value Address (val)
Example
prof. Marco Bellia, Dip. Informatica, Università di Pisa
2014-03-01 11:04:09
Lectures 3-4-5-6 Basics in Procedural Programming: Machinery
19/48
Lecture3-6E-2014.pdf (19/48)
Finding the Right Binding: The Simple Approach
Q: How can we finding the right binding of an identifier (during
program execution)?
A: By using the active AR in a backward visit of the AR frames
along:
(Static Scope) the Static Chain (cs if procedures / cd if inline)
(Dynamic Scope) the Dynamic Chain (cd)
and stopping when a binding for the identifier is found.
the found binding, if any, is the right binding of the identifier.
Example
prof. Marco Bellia, Dip. Informatica, Università di Pisa
2014-03-01 11:04:09
Lectures 3-4-5-6 Basics in Procedural Programming: Machinery
20/48
Lecture3-6E-2014.pdf (20/48)
Finding the Right Binding: Le Blank - Cook Approach
The simple approach requires O(n*p) accesses and
comparisons (for n-sized frames / p-sized chain lenghts)
Le Blank - Cook (1983) is only for Static Scope
It reduces the finding cost to O(p) (and by using, display
vector to O(1))
It consits in:
To each identifier I that is used in a block B it associates a
pair [l,p]:
l = is called Static Chain Link and is equal to the number of
nestings of B w.r. to the block A containing the binding of I.
l=0 means the 0-nesting( level)s – Noting that, procedure
blocks that are declared in a block are considered as nested in
such a block.
p = is called position and is equal to the position, from the
top, in the frame of A (above), of the binding of I.
It replaces, identifiers, everywhere are used, with their pair
[l,p], above.
prof. Marco Bellia, Dip. Informatica, Università di Pisa
2014-03-01 11:04:09
Lectures 3-4-5-6 Basics in Procedural Programming: Machinery
21/48
Lecture3-6E-2014.pdf (21/48)
Le Blank - Cook (1983): Examples
Le Blank - Cook is only for Static Scope
It reduces the finding cost to O(p) (and by using, display
vector to O(1))
It replaces, identifiers, everywhere are used, with their pair
[l,p], above.
Example
prof. Marco Bellia, Dip. Informatica, Università di Pisa
2014-03-01 11:04:09
Lectures 3-4-5-6 Basics in Procedural Programming: Machinery
22/48
Lecture3-6E-2014.pdf (22/48)
Le Blank - Cook (1983): Examples/2
Example
Noting the use of display vectors, in red lines/boxes, in the image
on the right side.
prof. Marco Bellia, Dip. Informatica, Università di Pisa
2014-03-01 11:04:09
Lectures 3-4-5-6 Basics in Procedural Programming: Machinery
23/48
Lecture3-6E-2014.pdf (23/48)
Programming Units
Programming Units = Each Section that is able to completely
describe one (or more) functionalities of the algorithm that
the program expresses.
procedure, function, inline block, module, package,
abstraction, class... are candidates for Programming Units
but each of them is or not, a P.U. depending on various
factors, including the way in which bindings are used
Example
We must write a program for an algorithm A which:
- First, it reads a sequence of student applications: To do it for instance, A suggests
the use of an algorithm N that reads the anagraphic data and a different one, let us
say M, that reads the academic data;
- Then, it sorts the collected data: Again, to do it (the algorithm or) the programmer
suggests to use of a sorting algorithm B that requires some suitable representation
conversion algorithms, let us say N’ and M’;
- Then, ... but we stop here the history.
prof. Marco Bellia, Dip. Informatica, Università di Pisa
2014-03-01 11:04:09
Lectures 3-4-5-6 Basics in Procedural Programming: Machinery
24/48
Lecture3-6E-2014.pdf (24/48)
Programming Units: Apply them
We must write a program for an algorithm A which:
- First, it reads a sequence of student applications: To do it for instance, A suggests
the use of an algorithm N that reads the anagraphic data and a different one, let us
say M, that reads the academic data;
- Then, it sorts the collected data: Again, to do it (the algorithm or) the programmer
suggests to use of a sorting algorithm B that requires some suitable representation
conversion algorithms, let us say N’ and M’;
- Then, ... but we stop here the history.
Example
prof. Marco Bellia, Dip. Informatica, Università di Pisa
2014-03-01 11:04:09
Lectures 3-4-5-6 Basics in Procedural Programming: Machinery
25/48
Lecture3-6E-2014.pdf (25/48)
Sectioning and Scope in C: A Case Study
Procedures of C may contain only non-locals that must be globals of the module
Q: How can such a constraint be imposed in C?
What about its implications in:
Q: Programming?
A: Procedures are not Programming Units, in C
Q: Implementation?
A: Static Chain is ever pointing to the module global frame
Example
prof. Marco Bellia, Dip. Informatica, Università di Pisa
2014-03-01 11:04:09
Lectures 3-4-5-6 Basics in Procedural Programming: Machinery
26/48
Lecture3-6E-2014.pdf (26/48)
Sectioning and Scope in C: A Case Study/2
Procedures of C may contain non-locals that must be globals of the module
Q: How can such a constraint be imposed in C?
A: Blocks (included procedures) cannot introduce naming for local procedures
What about its implications in:
Q: Programming?
A: Procedures are not Programming Units, in C
Q: Implementation?
A: Static Chain is ever pointing to the module global frame
Hence: pointer cs can be dropped from AR’s of C
Hence: pairs [l,p], in the code of C procedures, have l equals to
either 0 (for local) or -1 (for global).
Example
prof. Marco Bellia, Dip. Informatica, Università di Pisa
2014-03-01 11:04:10
Lectures 3-4-5-6 Basics in Procedural Programming: Machinery
27/48
Lecture3-6E-2014.pdf (27/48)
Env: Aliasing
Env: It is at the basis of the mechanism of Naming
Naming: It allows
Use of name instead of Den(otable) Values
Sharing of Den(otable) Values, i.e. Aliasing
Aliasing = Different Names for the same Den Value
Constructs introducing Aliasing are:
Parameter Transmission: By Reference
Alias: x = alias y (unix Bash)
Example
prof. Marco Bellia, Dip. Informatica, Università di Pisa
2014-03-01 11:04:10
Lectures 3-4-5-6 Basics in Procedural Programming: Machinery
28/48
Lecture3-6E-2014.pdf (28/48)
Lambda Lifting and Dynamic Scope
Dynamic Scope = It may be overcome by using LL
LL = Technique for the Elimination of the free variables [i.e. non-local
parameters] in (functional) L.P. with Dynamic Scope
The ”non-locals” become additional parameters of the procedures
Transmission of ”non-locals” is
By Reference, or
by using Pointers (as in the C example, below)
Example
prof. Marco Bellia, Dip. Informatica, Università di Pisa
2014-03-01 11:04:10
Lectures 3-4-5-6 Basics in Procedural Programming: Machinery
29/48
Lecture3-6E-2014.pdf (29/48)
Lambda Lifting and Dynamic Scope/2
Example
The C program, on right side,
has been obtained by lambda lifting: Hence its procedures do
not contain non-locals
computes as the program on the left side but according to
dynamic scope.
Can you give the same but according to static scope?
The answer is NO (in C, it cannot be done), but...
prof. Marco Bellia, Dip. Informatica, Università di Pisa
2014-03-01 11:04:10
Lectures 3-4-5-6 Basics in Procedural Programming: Machinery
30/48
Lecture3-6E-2014.pdf (30/48)
Lambda Lifting and Dynamic Scope/3
Example
Can you give the same but according to static scope?
The answer is NO (in C, it cannot be done), but it can be done in Ocaml, below
Example
prof. Marco Bellia, Dip. Informatica, Università di Pisa
2014-03-01 11:04:10
Lectures 3-4-5-6 Basics in Procedural Programming: Machinery
31/48
Lecture3-6E-2014.pdf (31/48)
Closures
Closure = Code enclosing its Non-local Bindings
Semantic View: pair <code,non-locals bindings>
Syntactic View: functions returned as values from functions
Example
prof. Marco Bellia, Dip. Informatica, Università di Pisa
2014-03-01 11:04:10
Lectures 3-4-5-6 Basics in Procedural Programming: Machinery
32/48
Lecture3-6E-2014.pdf (32/48)
Env: Formalization and Implementation
Formalization. Env = Structure defined by the following operations
bind: Ide x Den x Env → Env
abstract view:
bind(i,d,e) = λu. if (u=i) then d else e(u) – a function
concrete view:
bind(i,d,e) = (i,d)::e – a pair list
find: Ide x Env → (Den + Ide)
abstract view:
find(i,e) =e(i) – a function application
concrete view:
find(i,e) = match e with
| [] → i
| (u,dj )::er → if (u=i) then d else find(i,er)
empty: () → Env
abstract view:
empty() = λ u. u – identity
concrete view:
empty() = [] – empty list
Implementation. A frame similar to the one used in AR
prof. Marco Bellia, Dip. Informatica, Università di Pisa
2014-03-01 11:04:10
Lectures 3-4-5-6 Basics in Procedural Programming: Machinery
33/48
Lecture3-6E-2014.pdf (33/48)
Env: Formalization and Implementation in Summary
Formalization. Env = Structure defined by the following operations
bind: Ide x Den x Env → Env
find: Ide x Env → (Den + Ide)
empty: () → Env
Implementation is a frame similar to the one used in AR
A pictorial representation is below
Example
prof. Marco Bellia, Dip. Informatica, Università di Pisa
2014-03-01 11:04:10
Lectures 3-4-5-6 Basics in Procedural Programming: Machinery
34/48
Lecture3-6E-2014.pdf (34/48)
Store: Formalization and Implementation
Formalization. Store = Structure defined by the following operations
new: Mem x Store → Loc x Store
allocation operations are possibly, more than one according to
language store features
upd: Loc x Mem x Store → Store
abstract view: ....
look: Loc x Store → Mem
abstract view: ...
Implementation. 3 different main kinds of store:
Static: The Standard Store of Machine Languages
Stack: The supporting store of block based Programming Languages
(with recursive procedures)
Dynamic: In almost all today Programming Languages.
prof. Marco Bellia, Dip. Informatica, Università di Pisa
2014-03-01 11:04:10
Lectures 3-4-5-6 Basics in Procedural Programming: Machinery
35/48
Lecture3-6E-2014.pdf (35/48)
Stack: Implementation/1
It is built on a section of (contiguous “words” of) static memory
Pointer Start is pointing to the first “word” of the section
Pointer Top is pointing to the Stack Top,
(in case of AR Stack) Pointer Access is pointing to the Access Point of the AR
in the top.
Example
prof. Marco Bellia, Dip. Informatica, Università di Pisa
2014-03-01 11:04:10
Lectures 3-4-5-6 Basics in Procedural Programming: Machinery
36/48
Lecture3-6E-2014.pdf (36/48)
Stack: Implementation/2
The role of the Stack for Intermediate Results in a program
for JVM
Example
prof. Marco Bellia, Dip. Informatica, Università di Pisa
2014-03-01 11:04:10
Lectures 3-4-5-6 Basics in Procedural Programming: Machinery
37/48
Lecture3-6E-2014.pdf (37/48)
Heap: Implementation
It is built on a section of (contiguous “words” of) static store
Two kinds of Heap:
Homogeneous Heap: Blocks of one only size
Variable Heap: Blocks of different sizes
Allocation may reserve only contiguous blocks
Example
prof. Marco Bellia, Dip. Informatica, Università di Pisa
2014-03-01 11:04:10
Lectures 3-4-5-6 Basics in Procedural Programming: Machinery
38/48
Lecture3-6E-2014.pdf (38/48)
Heap: Implementation/2
It is built on a section of (contiguous “words” of) static store
Two kinds of Heap:
Homogeneous Heap: All blocks have fixed size k (words)
Memory is sectioned in blocks of k+1
The additional word is for pointing the next free block
Pointer LL is always pointing to the first block that is free for
allocation
Example
prof. Marco Bellia, Dip. Informatica, Università di Pisa
2014-03-01 11:04:11
Lectures 3-4-5-6 Basics in Procedural Programming: Machinery
39/48
Lecture3-6E-2014.pdf (39/48)
Heap: Fragmentation/1
Two kinds of Heap:
Homogeneous Heap: All blocks have fixed size k (words)
Variable Heap: (see later on)
Fragmentation: A problem in Unused Memory
Internal Fragmentation: Cannot be avoided
External Fragmentation: Of dramatic relevance
Example
prof. Marco Bellia, Dip. Informatica, Università di Pisa
2014-03-01 11:04:11
Lectures 3-4-5-6 Basics in Procedural Programming: Machinery
40/48
Lecture3-6E-2014.pdf (40/48)
Heap: Fragmentation/2
Two kinds of Heap:
Homogeneous Heap: All blocks have fixed size k (words)
Variable Heap: (see later on)
Fragmentation: A problem in Unused Memory
Internal Fragmentation: Cannot be avoided
External Fragmentation: Of dramatic relevance in both kind of
Heaps
Example
prof. Marco Bellia, Dip. Informatica, Università di Pisa
2014-03-01 11:04:11
Lectures 3-4-5-6 Basics in Procedural Programming: Machinery
41/48
Lecture3-6E-2014.pdf (41/48)
Heap: Implementation/3
It is built on a section of (contiguous “words” of) static store
Two kinds of Heap:
Homogeneous Heap: (see previous slides)
Variable Heap: Blocks of different sizes.
Two main structures:
Single list: e.g. Best Fit allocation
Multiple List: Buddy or Fibonacci allocation
prof. Marco Bellia, Dip. Informatica, Università di Pisa
2014-03-01 11:04:11
Lectures 3-4-5-6 Basics in Procedural Programming: Machinery
42/48
Lecture3-6E-2014.pdf (42/48)
Heap: Implementation/4 (Best Fit)
Best Fit allocation: The Free (Single) List is scanned for
finding the minimum size free block larger than the section to
be allocated
Compaction may merge contiguous blocks into one block
Example
prof. Marco Bellia, Dip. Informatica, Università di Pisa
2014-03-01 11:04:11
Lectures 3-4-5-6 Basics in Procedural Programming: Machinery
43/48
Lecture3-6E-2014.pdf (43/48)
Heap: Implementation/5 (Multiple Lists)
Multiple (Homo-)lists:
[k1 ],[k2 ],...,[kn ] = n homo-lists of different sizes
k1 < k2 < ... < kn
Section of size k is allocated in list [ki ] such that:
ki−1 < k < ki
Directly: If [ki ] has a free block
Through list [ki+j ]: If [ki+j ] has a free block and ki+j is such
that: Let p=i+j-1, then
(m1,...,mp) exists: m1*ki +...+mp*kp =ki+j
One block B of [ki+j ] is then broken in (m1,m2,...,mp) blocks
of the right sizes
Lists [ki ],...,[kp ] are extended with the new blocks
Allocation of Section of size k then applies
Block B is re-built once: m1 blocks of list [ki ],..., mp blocks
of list [kp ] are become free.
Critical is: The choice of the block size in the lists
Buddy: k1 < k2 < ... < kn are (contiguous) powers of 2
Fibonacci: k1 < k2 < ... < kn are (contiguous) Fibonacci’s
numbers
prof. Marco Bellia, Dip. Informatica, Università di Pisa
2014-03-01 11:04:11
Lectures 3-4-5-6 Basics in Procedural Programming: Machinery
44/48
Lecture3-6E-2014.pdf (44/48)
Heap: Implementation/6
Buddy: k1 < k2 < ... < kn are (contiguous) powers of 2
Fibonacci: k1 < k2 < ... < kn are (contiguous) Fibonacci’s
numbers
Example
prof. Marco Bellia, Dip. Informatica, Università di Pisa
2014-03-01 11:04:11
Lectures 3-4-5-6 Basics in Procedural Programming: Machinery
45/48
Lecture3-6E-2014.pdf (45/48)
Heap: Compaction - An Exercise/7
Example
Compaction is an Heap operation for allowing that contiguous, free blocks may be
allocated at once. In programming languages with pointers as values, the operation is
acting on a rearrangement of the free list and in no case, it moves values that are in
use, in a block, into a different block.
a. Discuss the reasons of it and show an example that illustrates such points.
b. Using the example in (a), discuss the problem in Languages which have not
pointers as values
b. Discuss an algorithm (or write a program) for the re-arrangement of the free
list in a homo-heap
Answer (a): Moving memory that contains absolute values does not present any
problem. In contrast, when the memory contains relocatable values, to move memory
it is needed to know (and bring along, the relocation base). Consider the code below
and suppose that x is allocated in a block X and Y is allocated in a block Y.
... x = malloc(k*sizeof(int));...
... y = malloc(k*sizeof(int));...
... z = x > y ? x : y; ...
What is the value of z if X and Y are relocated immediately after the assignment of z?
Can You say that the relocation of X and Y is not affecting the behavior of the
program?
prof. Marco Bellia, Dip. Informatica, Università di Pisa
2014-03-01 11:04:11
Lectures 3-4-5-6 Basics in Procedural Programming: Machinery
46/48
Lecture3-6E-2014.pdf (46/48)
Comparison Table
Environment and Memory in: Fortran, C, Pascal, Caml, Java,
Prolog
Example
prof. Marco Bellia, Dip. Informatica, Università di Pisa
2014-03-01 11:04:11
Lectures 3-4-5-6 Basics in Procedural Programming: Machinery
47/48
Lecture3-6E-2014.pdf (47/48)
Things To Do
STUDY
in deeper way, the content of the slides. You can read in
addition, the chapters 4 and 5 of the book by Gabrielli and
Martini before mentioned, and use the bibliographic references
in the chapter for additional readings.
PROGRAMMING
check the installation of the tools for the use of following
languages: C, Java, Caml, Prolog (Use the first 3 in giving a
program for the computation of factorial)
EXERCISES:
verify, complete and extend all the examples and/or exercises
included in the slides by using the right tools (programs and
languages)
consider the exercises included in the chapters 4 and 5 of the
book by Gabrielli and Martini.
prof. Marco Bellia, Dip. Informatica, Università di Pisa
2014-03-01 11:04:11
Lectures 3-4-5-6 Basics in Procedural Programming: Machinery
48/48
Lecture3-6E-2014.pdf (48/48)
© Copyright 2026 Paperzz