CSCI 3136 Principles of Programming Languages Names, Scopes, and - - PowerPoint PPT Presentation

csci 3136 principles of programming languages
SMART_READER_LITE
LIVE PREVIEW

CSCI 3136 Principles of Programming Languages Names, Scopes, and - - PowerPoint PPT Presentation

CSCI 3136 Principles of Programming Languages Names, Scopes, and Bindings - 1 Summer 2013 Faculty of Computer Science Dalhousie University 1 / 87 Name A name is a mnemonic character string representing something else: 2 / 87 Name A name is


slide-1
SLIDE 1

CSCI 3136 Principles of Programming Languages

Names, Scopes, and Bindings - 1

Summer 2013 Faculty of Computer Science Dalhousie University

1 / 87

slide-2
SLIDE 2

Name

A name is a mnemonic character string representing something else:

2 / 87

slide-3
SLIDE 3

Name

A name is a mnemonic character string representing something else: For example:

  • x,sin,f,prog1,null? are names

3 / 87

slide-4
SLIDE 4

Name

A name is a mnemonic character string representing something else: For example:

  • x,sin,f,prog1,null? are names
  • 1,2,3, ‘‘test’’ are not names

4 / 87

slide-5
SLIDE 5

Name

A name is a mnemonic character string representing something else: For example:

  • x,sin,f,prog1,null? are names
  • 1,2,3, ‘‘test’’ are not names
  • +, <=, . . . may be names, if they are not built-in operators

5 / 87

slide-6
SLIDE 6

Binding

A binding is an association between two entities:

6 / 87

slide-7
SLIDE 7

Binding

A binding is an association between two entities: For example:

  • Name and a memory location (for variables)

7 / 87

slide-8
SLIDE 8

Binding

A binding is an association between two entities: For example:

  • Name and a memory location (for variables)
  • Name and a function

8 / 87

slide-9
SLIDE 9

Binding

A binding is an association between two entities: For example:

  • Name and a memory location (for variables)
  • Name and a function

Typically a binding is between a name and the object it refers to.

9 / 87

slide-10
SLIDE 10

Binding

A binding is an association between two entities: For example:

  • Name and a memory location (for variables)
  • Name and a function

Typically a binding is between a name and the object it refers to. A referencing environment is a complete set of bindings active at a certain point in a program.

10 / 87

slide-11
SLIDE 11

Scope

11 / 87

slide-12
SLIDE 12

Scope

  • Scope of a binding

is the region of a program, or time interval(s) in the program’s execution during which the binding is active.

12 / 87

slide-13
SLIDE 13

Scope

  • Scope of a binding

is the region of a program, or time interval(s) in the program’s execution during which the binding is active.

  • Scope

is a maximal region of the program where no bindings are destroyed (e.g., body of a procedure).

13 / 87

slide-14
SLIDE 14

Binding Times

Compile time

  • Mapping of high-level language constructs to machine code
  • Layout of static data in memory

14 / 87

slide-15
SLIDE 15

Binding Times

Compile time

  • Mapping of high-level language constructs to machine code
  • Layout of static data in memory

Link time

  • Resolve references between separately compiled modules

15 / 87

slide-16
SLIDE 16

Binding Times

Compile time

  • Mapping of high-level language constructs to machine code
  • Layout of static data in memory

Link time

  • Resolve references between separately compiled modules

Load time

  • Assign machine addresses to static data

16 / 87

slide-17
SLIDE 17

Binding Times

Compile time

  • Mapping of high-level language constructs to machine code
  • Layout of static data in memory

Link time

  • Resolve references between separately compiled modules

Load time

  • Assign machine addresses to static data

Run time

  • Binding of values to variables
  • Allocate local variables of procedures on the stack
  • Allocate dynamic data and assign to variables

17 / 87

slide-18
SLIDE 18

Importance of Binding Time

Early binding

  • Faster code
  • Typical in compiled languages

18 / 87

slide-19
SLIDE 19

Importance of Binding Time

Early binding

  • Faster code
  • Typical in compiled languages

Late binding

  • Greater flexibility
  • Typical in interpreted languages

19 / 87

slide-20
SLIDE 20

Object and Binding Lifetime

Object lifetime

  • Period between the creation and destruction of an object
  • Example:

− time between creation and destruction of a dynamically allocated variable in C++ using new and delete − pushing and popping a stack frame

20 / 87

slide-21
SLIDE 21

Object and Binding Lifetime

Object lifetime

  • Period between the creation and destruction of an object
  • Example:

− time between creation and destruction of a dynamically allocated variable in C++ using new and delete − pushing and popping a stack frame

Binding lifetime

  • Period between the creation and destruction of the binding

(name-to-object association)

21 / 87

slide-22
SLIDE 22

Object and Binding Lifetime

Object lifetime

  • Period between the creation and destruction of an object
  • Example:

− time between creation and destruction of a dynamically allocated variable in C++ using new and delete − pushing and popping a stack frame

Binding lifetime

  • Period between the creation and destruction of the binding

(name-to-object association) Two common mistakes

  • Dangling reference: no object for a binding (e.g., a pointer

refers to an object that has already been deleted)

  • Memory leak: no binding for an object (preventing the object

from being deallocated)

22 / 87

slide-23
SLIDE 23

Storage Allocation

An object’s lifetime corresponds to the mechanism used to manage the space where the object resides.

23 / 87

slide-24
SLIDE 24

Storage Allocation

An object’s lifetime corresponds to the mechanism used to manage the space where the object resides. Static object

  • Object stored at a fixed absolute address
  • Object’s lifetime spans the whole execution of the program

24 / 87

slide-25
SLIDE 25

Storage Allocation

An object’s lifetime corresponds to the mechanism used to manage the space where the object resides. Static object

  • Object stored at a fixed absolute address
  • Object’s lifetime spans the whole execution of the program

Object on stack

  • Object allocated on stack in connection with a subroutine call
  • Object’s lifetime spans period between invocation of the

subroutine and return from the subroutine

25 / 87

slide-26
SLIDE 26

Storage Allocation

An object’s lifetime corresponds to the mechanism used to manage the space where the object resides. Static object

  • Object stored at a fixed absolute address
  • Object’s lifetime spans the whole execution of the program

Object on stack

  • Object allocated on stack in connection with a subroutine call
  • Object’s lifetime spans period between invocation of the

subroutine and return from the subroutine Object on heap

  • Object stored on heap
  • Object created/destroyed at arbitrary times

− Explicitly by programmer or − Implicitly by garbage collector

26 / 87

slide-27
SLIDE 27

Example: Object Creation and Destruction in C++

27 / 87

slide-28
SLIDE 28

Example: Object Creation and Destruction in C++

  • Local objects are local to functions and blocks and exist while

the execution is inside the function or block.

28 / 87

slide-29
SLIDE 29

Example: Object Creation and Destruction in C++

  • Local objects are local to functions and blocks and exist while

the execution is inside the function or block.

  • Heap objects are allocated/deallocated using new/delete.

(free-store objects)

29 / 87

slide-30
SLIDE 30

Example: Object Creation and Destruction in C++

  • Local objects are local to functions and blocks and exist while

the execution is inside the function or block.

  • Heap objects are allocated/deallocated using new/delete.

(free-store objects)

  • Non-static member objects of a parent object exist while the

parent object exists.

30 / 87

slide-31
SLIDE 31

Example: Object Creation and Destruction in C++

  • Local objects are local to functions and blocks and exist while

the execution is inside the function or block.

  • Heap objects are allocated/deallocated using new/delete.

(free-store objects)

  • Non-static member objects of a parent object exist while the

parent object exists.

  • Array elements exist while the array exists.

31 / 87

slide-32
SLIDE 32

Example: Object Creation and Destruction in C++

  • Local objects are local to functions and blocks and exist while

the execution is inside the function or block.

  • Heap objects are allocated/deallocated using new/delete.

(free-store objects)

  • Non-static member objects of a parent object exist while the

parent object exists.

  • Array elements exist while the array exists.
  • Local static objects of functions/blocks exist after the first

invocation of the function until termination.

32 / 87

slide-33
SLIDE 33

Example: Object Creation and Destruction in C++

  • Local objects are local to functions and blocks and exist while

the execution is inside the function or block.

  • Heap objects are allocated/deallocated using new/delete.

(free-store objects)

  • Non-static member objects of a parent object exist while the

parent object exists.

  • Array elements exist while the array exists.
  • Local static objects of functions/blocks exist after the first

invocation of the function until termination.

  • Global, namespace, class static objects exist while the

program runs.

33 / 87

slide-34
SLIDE 34

Example: Object Creation and Destruction in C++

  • Local objects are local to functions and blocks and exist while

the execution is inside the function or block.

  • Heap objects are allocated/deallocated using new/delete.

(free-store objects)

  • Non-static member objects of a parent object exist while the

parent object exists.

  • Array elements exist while the array exists.
  • Local static objects of functions/blocks exist after the first

invocation of the function until termination.

  • Global, namespace, class static objects exist while the

program runs.

  • Temporary objects in expressions exist during the evaluation
  • f the expression.

34 / 87

slide-35
SLIDE 35

Static Objects

35 / 87

slide-36
SLIDE 36

Static Objects

  • Global variables

36 / 87

slide-37
SLIDE 37

Static Objects

  • Global variables
  • Variables local to subroutines, but retain their value between

invocations

37 / 87

slide-38
SLIDE 38

Static Objects

  • Global variables
  • Variables local to subroutines, but retain their value between

invocations

  • Constant literals

38 / 87

slide-39
SLIDE 39

Static Objects

  • Global variables
  • Variables local to subroutines, but retain their value between

invocations

  • Constant literals
  • Tables for run-time support: e.g., debugging, type checking,

etc.

39 / 87

slide-40
SLIDE 40

Static Objects

  • Global variables
  • Variables local to subroutines, but retain their value between

invocations

  • Constant literals
  • Tables for run-time support: e.g., debugging, type checking,

etc.

  • Space for subroutines, including local variables in languages

that do not support recursion (e.g., early versions of FORTRAN)

40 / 87

slide-41
SLIDE 41

Stack-Based Allocation

The stack is used to allocate space for subroutines in languages that permit recursion.

41 / 87

slide-42
SLIDE 42

Stack-Based Allocation

The stack is used to allocate space for subroutines in languages that permit recursion. The stack frame (activation record) contains

  • Arguments and local variables of the subroutine,
  • The return value(s) of the subroutine,
  • The return address, etc.

42 / 87

slide-43
SLIDE 43

Stack-Based Allocation

The stack is used to allocate space for subroutines in languages that permit recursion. The stack frame (activation record) contains

  • Arguments and local variables of the subroutine,
  • The return value(s) of the subroutine,
  • The return address, etc.

The subroutine calling sequence maintains the stack:

  • Before call, the caller pushes arguments and return address
  • nto the stack.
  • After being called (prologue), the subroutine (callee) initializes

local variables, etc.

  • Before returning (epilogue), the subroutine cleans up local

data.

  • After the call returns, the caller retrieves return value(s) and

restores the stack to its state before the call.

43 / 87

slide-44
SLIDE 44

Stack Frame (Activation Record)

44 / 87

slide-45
SLIDE 45

Stack Frame (Activation Record)

Compiler determines

  • Frame pointer: a register pointing to a known location within

the current stack frame

  • Offsets from the frame pointer specifying the location of
  • bjects in the stack frame

45 / 87

slide-46
SLIDE 46

Stack Frame (Activation Record)

Compiler determines

  • Frame pointer: a register pointing to a known location within

the current stack frame

  • Offsets from the frame pointer specifying the location of
  • bjects in the stack frame

The absolute size of the stack frame may not be known at compile time (e.g., variable-size arrays allocated on the stack).

46 / 87

slide-47
SLIDE 47

Stack Frame (Activation Record)

Compiler determines

  • Frame pointer: a register pointing to a known location within

the current stack frame

  • Offsets from the frame pointer specifying the location of
  • bjects in the stack frame

The absolute size of the stack frame may not be known at compile time (e.g., variable-size arrays allocated on the stack). Stack pointer

  • Register pointing to the first unused location on the stack

(used as the starting position for the next stack frame)

47 / 87

slide-48
SLIDE 48

Stack Frame (Activation Record)

Compiler determines

  • Frame pointer: a register pointing to a known location within

the current stack frame

  • Offsets from the frame pointer specifying the location of
  • bjects in the stack frame

The absolute size of the stack frame may not be known at compile time (e.g., variable-size arrays allocated on the stack). Stack pointer

  • Register pointing to the first unused location on the stack

(used as the starting position for the next stack frame) Specified at runtime

  • The absolute location of the stack frame in memory (on the

stack)

  • The size of the stack frame

48 / 87

slide-49
SLIDE 49

Stack Frame Before, During and After Subroutine Call

49 / 87

slide-50
SLIDE 50

Heap-Based Allocation

The heap is a region of memory where subblocks can be allocated and deallocated at arbitrary times and in arbitrary order.

50 / 87

slide-51
SLIDE 51

Heap-Based Allocation

The heap is a region of memory where subblocks can be allocated and deallocated at arbitrary times and in arbitrary order. Heap management

  • Free list: linked list of free blocks (of memory)
  • The allocation algorithm searches for a block of adequate size

to accommodate the allocation request.

51 / 87

slide-52
SLIDE 52

First-Fit and Best-Fit Allocation

  • First fit: grab first block that is large enough
  • Best fit: grab smallest block that is large enough

52 / 87

slide-53
SLIDE 53

Heap Fragmentation Problem

Internal fragmentation

  • part of a block is unused

53 / 87

slide-54
SLIDE 54

Heap Fragmentation Problem

Internal fragmentation

  • part of a block is unused

External fragmentation

  • unused space consists of many small blocks
  • although total free space may exceed allocation request,

individual free blocks may be too small

54 / 87

slide-55
SLIDE 55

Heap Fragmentation Problem

Internal fragmentation

  • part of a block is unused

External fragmentation

  • unused space consists of many small blocks
  • although total free space may exceed allocation request,

individual free blocks may be too small Is there less external fragmentation with “best fit” or “first fit”?

55 / 87

slide-56
SLIDE 56

Heap Fragmentation Problem

Internal fragmentation

  • part of a block is unused

External fragmentation

  • unused space consists of many small blocks
  • although total free space may exceed allocation request,

individual free blocks may be too small Is there less external fragmentation with “best fit” or “first fit”? Neither best-fit nor first-fit is guaranteed to minimize external

  • fragmentation. Which strategy is better depends on the size

distribution of the allocation requests.

56 / 87

slide-57
SLIDE 57

Cost of Allocation on a Heap

Single free list

  • linear cost in the number of free blocks

57 / 87

slide-58
SLIDE 58

Cost of Allocation on a Heap

Single free list

  • linear cost in the number of free blocks

Buddy system

  • Block sizes are powers of 2
  • Separate free list for blocks of size

2k, for each k

  • If block of size 2k is unavailable,

split block of size 2k+1

  • If block of size 2k is deallocated

and its buddy is free, merge them into a block of size 2k+1

  • Worst-case cost: log(memory size)

58 / 87

slide-59
SLIDE 59

Cost of Allocation on a Heap

Single free list

  • linear cost in the number of free blocks

Buddy system

  • Block sizes are powers of 2
  • Separate free list for blocks of size

2k, for each k

  • If block of size 2k is unavailable,

split block of size 2k+1

  • If block of size 2k is deallocated

and its buddy is free, merge them into a block of size 2k+1

  • Worst-case cost: log(memory size)

59 / 87

slide-60
SLIDE 60

Cost of Allocation on a Heap

Single free list

  • linear cost in the number of free blocks

Buddy system

  • Block sizes are powers of 2
  • Separate free list for blocks of size

2k, for each k

  • If block of size 2k is unavailable,

split block of size 2k+1

  • If block of size 2k is deallocated

and its buddy is free, merge them into a block of size 2k+1

  • Worst-case cost: log(memory size)

60 / 87

slide-61
SLIDE 61

Cost of Allocation on a Heap

Single free list

  • linear cost in the number of free blocks

Buddy system

  • Block sizes are powers of 2
  • Separate free list for blocks of size

2k, for each k

  • If block of size 2k is unavailable,

split block of size 2k+1

  • If block of size 2k is deallocated

and its buddy is free, merge them into a block of size 2k+1

  • Worst-case cost: log(memory size)

61 / 87

slide-62
SLIDE 62

Cost of Allocation on a Heap

Single free list

  • linear cost in the number of free blocks

Buddy system

  • Block sizes are powers of 2
  • Separate free list for blocks of size

2k, for each k

  • If block of size 2k is unavailable,

split block of size 2k+1

  • If block of size 2k is deallocated

and its buddy is free, merge them into a block of size 2k+1

  • Worst-case cost: log(memory size)

62 / 87

slide-63
SLIDE 63

Cost of Allocation on a Heap

Single free list

  • linear cost in the number of free blocks

Buddy system

  • Block sizes are powers of 2
  • Separate free list for blocks of size

2k, for each k

  • If block of size 2k is unavailable,

split block of size 2k+1

  • If block of size 2k is deallocated

and its buddy is free, merge them into a block of size 2k+1

  • Worst-case cost: log(memory size)

63 / 87

slide-64
SLIDE 64

Cost of Allocation on a Heap

Single free list

  • linear cost in the number of free blocks

Buddy system

  • Block sizes are powers of 2
  • Separate free list for blocks of size

2k, for each k

  • If block of size 2k is unavailable,

split block of size 2k+1

  • If block of size 2k is deallocated

and its buddy is free, merge them into a block of size 2k+1

  • Worst-case cost: log(memory size)

64 / 87

slide-65
SLIDE 65

Cost of Allocation on a Heap

Single free list

  • linear cost in the number of free blocks

Buddy system

  • Block sizes are powers of 2
  • Separate free list for blocks of size

2k, for each k

  • If block of size 2k is unavailable,

split block of size 2k+1

  • If block of size 2k is deallocated

and its buddy is free, merge them into a block of size 2k+1

  • Worst-case cost: log(memory size)

65 / 87

slide-66
SLIDE 66

Cost of Allocation on a Heap

Single free list

  • linear cost in the number of free blocks

Buddy system

  • Block sizes are powers of 2
  • Separate free list for blocks of size

2k, for each k

  • If block of size 2k is unavailable,

split block of size 2k+1

  • If block of size 2k is deallocated

and its buddy is free, merge them into a block of size 2k+1

  • Worst-case cost: log(memory size)

66 / 87

slide-67
SLIDE 67

Cost of Allocation on a Heap

Single free list

  • linear cost in the number of free blocks

Buddy system

  • Block sizes are powers of 2
  • Separate free list for blocks of size

2k, for each k

  • If block of size 2k is unavailable,

split block of size 2k+1

  • If block of size 2k is deallocated

and its buddy is free, merge them into a block of size 2k+1

  • Worst-case cost: log(memory size)

67 / 87

slide-68
SLIDE 68

Cost of Allocation on a Heap

Single free list

  • linear cost in the number of free blocks

Buddy system

  • Block sizes are powers of 2
  • Separate free list for blocks of size

2k, for each k

  • If block of size 2k is unavailable,

split block of size 2k+1

  • If block of size 2k is deallocated

and its buddy is free, merge them into a block of size 2k+1

  • Worst-case cost: log(memory size)

68 / 87

slide-69
SLIDE 69

Cost of Allocation on a Heap

Single free list

  • linear cost in the number of free blocks

Buddy system

  • Block sizes are powers of 2
  • Separate free list for blocks of size

2k, for each k

  • If block of size 2k is unavailable,

split block of size 2k+1

  • If block of size 2k is deallocated

and its buddy is free, merge them into a block of size 2k+1

  • Worst-case cost: log(memory size)

69 / 87

slide-70
SLIDE 70

Cost of Allocation on a Heap

Single free list

  • linear cost in the number of free blocks

Buddy system

  • Block sizes are powers of 2
  • Separate free list for blocks of size

2k, for each k

  • If block of size 2k is unavailable,

split block of size 2k+1

  • If block of size 2k is deallocated

and its buddy is free, merge them into a block of size 2k+1

  • Worst-case cost: log(memory size)

70 / 87

slide-71
SLIDE 71

Cost of Allocation on a Heap

Single free list

  • linear cost in the number of free blocks

Buddy system

  • Block sizes are powers of 2
  • Separate free list for blocks of size

2k, for each k

  • If block of size 2k is unavailable,

split block of size 2k+1

  • If block of size 2k is deallocated

and its buddy is free, merge them into a block of size 2k+1

  • Worst-case cost: log(memory size)

71 / 87

slide-72
SLIDE 72

Cost of Allocation on a Heap

Single free list

  • linear cost in the number of free blocks

Buddy system

  • Block sizes are powers of 2
  • Separate free list for blocks of size

2k, for each k

  • If block of size 2k is unavailable,

split block of size 2k+1

  • If block of size 2k is deallocated

and its buddy is free, merge them into a block of size 2k+1

  • Worst-case cost: log(memory size)

72 / 87

slide-73
SLIDE 73

Cost of Allocation on a Heap

Single free list

  • linear cost in the number of free blocks

Buddy system

  • Block sizes are powers of 2
  • Separate free list for blocks of size

2k, for each k

  • If block of size 2k is unavailable,

split block of size 2k+1

  • If block of size 2k is deallocated

and its buddy is free, merge them into a block of size 2k+1

  • Worst-case cost: log(memory size)

73 / 87

slide-74
SLIDE 74

Cost of Allocation on a Heap

Single free list

  • linear cost in the number of free blocks

Buddy system

  • Block sizes are powers of 2
  • Separate free list for blocks of size

2k, for each k

  • If block of size 2k is unavailable,

split block of size 2k+1

  • If block of size 2k is deallocated

and its buddy is free, merge them into a block of size 2k+1

  • Worst-case cost: log(memory size)

74 / 87

slide-75
SLIDE 75

Cost of Allocation on a Heap

Single free list

  • linear cost in the number of free blocks

Buddy system

  • Block sizes are powers of 2
  • Separate free list for blocks of size

2k, for each k

  • If block of size 2k is unavailable,

split block of size 2k+1

  • If block of size 2k is deallocated

and its buddy is free, merge them into a block of size 2k+1

  • Worst-case cost: log(memory size)

75 / 87

slide-76
SLIDE 76

Cost of Allocation on a Heap

Single free list

  • linear cost in the number of free blocks

Buddy system

  • Block sizes are powers of 2
  • Separate free list for blocks of size

2k, for each k

  • If block of size 2k is unavailable,

split block of size 2k+1

  • If block of size 2k is deallocated

and its buddy is free, merge them into a block of size 2k+1

  • Worst-case cost: log(memory size)

76 / 87

slide-77
SLIDE 77

Cost of Allocation on a Heap

Single free list

  • linear cost in the number of free blocks

Buddy system

  • Block sizes are powers of 2
  • Separate free list for blocks of size

2k, for each k

  • If block of size 2k is unavailable,

split block of size 2k+1

  • If block of size 2k is deallocated

and its buddy is free, merge them into a block of size 2k+1

  • Worst-case cost: log(memory size)

77 / 87

slide-78
SLIDE 78

Cost of Allocation on a Heap

Single free list

  • linear cost in the number of free blocks

Buddy system

  • Block sizes are powers of 2
  • Separate free list for blocks of size

2k, for each k

  • If block of size 2k is unavailable,

split block of size 2k+1

  • If block of size 2k is deallocated

and its buddy is free, merge them into a block of size 2k+1

  • Worst-case cost: log(memory size)

Fibonacci heap

  • Block sizes that are Fibonacci #:

1, 1, 2, 3, 5, 8, 13, 21, 34, . . .

  • Less fragmentation

78 / 87

slide-79
SLIDE 79

Deallocation on a Heap

Explicit deallocation by programmer Automatic deallocation by garbage collector

79 / 87

slide-80
SLIDE 80

Deallocation on a Heap

Explicit deallocation by programmer

  • Used in Pascal, C, C++, . . .

Automatic deallocation by garbage collector

80 / 87

slide-81
SLIDE 81

Deallocation on a Heap

Explicit deallocation by programmer

  • Used in Pascal, C, C++, . . .
  • Efficient

Automatic deallocation by garbage collector

81 / 87

slide-82
SLIDE 82

Deallocation on a Heap

Explicit deallocation by programmer

  • Used in Pascal, C, C++, . . .
  • Efficient
  • May lead to bugs that are difficult to find:

Automatic deallocation by garbage collector

82 / 87

slide-83
SLIDE 83

Deallocation on a Heap

Explicit deallocation by programmer

  • Used in Pascal, C, C++, . . .
  • Efficient
  • May lead to bugs that are difficult to find:

− Dangling pointers/references from deallocating too soon

Automatic deallocation by garbage collector

83 / 87

slide-84
SLIDE 84

Deallocation on a Heap

Explicit deallocation by programmer

  • Used in Pascal, C, C++, . . .
  • Efficient
  • May lead to bugs that are difficult to find:

− Dangling pointers/references from deallocating too soon − Memory leaks from not deallocating

Automatic deallocation by garbage collector

84 / 87

slide-85
SLIDE 85

Deallocation on a Heap

Explicit deallocation by programmer

  • Used in Pascal, C, C++, . . .
  • Efficient
  • May lead to bugs that are difficult to find:

− Dangling pointers/references from deallocating too soon − Memory leaks from not deallocating

Automatic deallocation by garbage collector

  • Used in Java, functional and logic programming languages, . . .

85 / 87

slide-86
SLIDE 86

Deallocation on a Heap

Explicit deallocation by programmer

  • Used in Pascal, C, C++, . . .
  • Efficient
  • May lead to bugs that are difficult to find:

− Dangling pointers/references from deallocating too soon − Memory leaks from not deallocating

Automatic deallocation by garbage collector

  • Used in Java, functional and logic programming languages, . . .
  • Can add significant runtime overhead

86 / 87

slide-87
SLIDE 87

Deallocation on a Heap

Explicit deallocation by programmer

  • Used in Pascal, C, C++, . . .
  • Efficient
  • May lead to bugs that are difficult to find:

− Dangling pointers/references from deallocating too soon − Memory leaks from not deallocating

Automatic deallocation by garbage collector

  • Used in Java, functional and logic programming languages, . . .
  • Can add significant runtime overhead
  • Safer

87 / 87