132
DATA TYPES & MEMORY MANAGEMENT PRINCIPLES OF PROGRAMMING LANGUAGES Norbert Zeh Winter 2018 Dalhousie University 1/41

Data Types & Memory Management - Principles of Programming ...nzeh/Teaching/3136/Slides/data-types.pdf · MEMORYLAYOUTOFRECORDS Aligned(fixedordering): name atomic_number atomic_weight

  • Upload
    others

  • View
    8

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Data Types & Memory Management - Principles of Programming ...nzeh/Teaching/3136/Slides/data-types.pdf · MEMORYLAYOUTOFRECORDS Aligned(fixedordering): name atomic_number atomic_weight

DATA TYPES & MEMORY MANAGEMENTPRINCIPLES OF PROGRAMMING LANGUAGES

Norbert ZehWinter 2018

Dalhousie University

1/41

Page 2: Data Types & Memory Management - Principles of Programming ...nzeh/Teaching/3136/Slides/data-types.pdf · MEMORYLAYOUTOFRECORDS Aligned(fixedordering): name atomic_number atomic_weight

DATA ABSTRACTION

Where subroutines allow us to build control abstractions, a language’s typesystem determines the kind of data abstractions we can build.

2/41

Page 3: Data Types & Memory Management - Principles of Programming ...nzeh/Teaching/3136/Slides/data-types.pdf · MEMORYLAYOUTOFRECORDS Aligned(fixedordering): name atomic_number atomic_weight

ROAD MAP

• Type Systems• Records• Arrays• Associative arrays• Pointers• Memory management• Garbage collection

3/41

Page 4: Data Types & Memory Management - Principles of Programming ...nzeh/Teaching/3136/Slides/data-types.pdf · MEMORYLAYOUTOFRECORDS Aligned(fixedordering): name atomic_number atomic_weight

ROAD MAP

• Type Systems• Records• Arrays• Associative arrays• Pointers• Memory management• Garbage collection

3/41

Page 5: Data Types & Memory Management - Principles of Programming ...nzeh/Teaching/3136/Slides/data-types.pdf · MEMORYLAYOUTOFRECORDS Aligned(fixedordering): name atomic_number atomic_weight

TYPE SYSTEMS

Type SystemMechanism for defining types and associating them with operations that can beperformed on objects of each type:

• Built-in types with built-in operations• Custom operations for built-in and custom types

A type system includes rules that specify

• Type equivalence: Do two values have the same type? (Structuralequivalence vs name equivalence)

• Type compatibility: Can a value of a certain type be used in a certaincontext?

• Type inference: How is the type of an expression computed from the types ofits parts?

4/41

Page 6: Data Types & Memory Management - Principles of Programming ...nzeh/Teaching/3136/Slides/data-types.pdf · MEMORYLAYOUTOFRECORDS Aligned(fixedordering): name atomic_number atomic_weight

TYPE SYSTEMS

Type SystemMechanism for defining types and associating them with operations that can beperformed on objects of each type:

• Built-in types with built-in operations• Custom operations for built-in and custom types

A type system includes rules that specify

• Type equivalence: Do two values have the same type? (Structuralequivalence vs name equivalence)

• Type compatibility: Can a value of a certain type be used in a certaincontext?

• Type inference: How is the type of an expression computed from the types ofits parts?

4/41

Page 7: Data Types & Memory Management - Principles of Programming ...nzeh/Teaching/3136/Slides/data-types.pdf · MEMORYLAYOUTOFRECORDS Aligned(fixedordering): name atomic_number atomic_weight

TYPE SYSTEMS

Type SystemMechanism for defining types and associating them with operations that can beperformed on objects of each type:

• Built-in types with built-in operations• Custom operations for built-in and custom types

A type system includes rules that specify

• Type equivalence: Do two values have the same type? (Structuralequivalence vs name equivalence)

• Type compatibility: Can a value of a certain type be used in a certaincontext?

• Type inference: How is the type of an expression computed from the types ofits parts?

4/41

Page 8: Data Types & Memory Management - Principles of Programming ...nzeh/Teaching/3136/Slides/data-types.pdf · MEMORYLAYOUTOFRECORDS Aligned(fixedordering): name atomic_number atomic_weight

TYPE SYSTEMS

Type SystemMechanism for defining types and associating them with operations that can beperformed on objects of each type:

• Built-in types with built-in operations• Custom operations for built-in and custom types

A type system includes rules that specify

• Type equivalence: Do two values have the same type? (Structuralequivalence vs name equivalence)

• Type compatibility: Can a value of a certain type be used in a certaincontext?

• Type inference: How is the type of an expression computed from the types ofits parts?

4/41

Page 9: Data Types & Memory Management - Principles of Programming ...nzeh/Teaching/3136/Slides/data-types.pdf · MEMORYLAYOUTOFRECORDS Aligned(fixedordering): name atomic_number atomic_weight

COMMON KINDS OF TYPE SYSTEMS

Strongly typedProhibits the application of an operation to any object not supporting thisoperation.

Statically typedStrongly typed and type checking is performed at compile time (Pascal, C,Haskell, …)

Dynamically typedStrongly typed and type checking is performed at runtime (LISP, Smalltalk,Python, …)

In some statically typed languages (e.g., ML), the programmer does not specifytypes at all. They are inferred by the compiler.

5/41

Page 10: Data Types & Memory Management - Principles of Programming ...nzeh/Teaching/3136/Slides/data-types.pdf · MEMORYLAYOUTOFRECORDS Aligned(fixedordering): name atomic_number atomic_weight

COMMON KINDS OF TYPE SYSTEMS

Strongly typedProhibits the application of an operation to any object not supporting thisoperation.

Statically typedStrongly typed and type checking is performed at compile time (Pascal, C,Haskell, …)

Dynamically typedStrongly typed and type checking is performed at runtime (LISP, Smalltalk,Python, …)

In some statically typed languages (e.g., ML), the programmer does not specifytypes at all. They are inferred by the compiler.

5/41

Page 11: Data Types & Memory Management - Principles of Programming ...nzeh/Teaching/3136/Slides/data-types.pdf · MEMORYLAYOUTOFRECORDS Aligned(fixedordering): name atomic_number atomic_weight

COMMON KINDS OF TYPE SYSTEMS

Strongly typedProhibits the application of an operation to any object not supporting thisoperation.

Statically typedStrongly typed and type checking is performed at compile time (Pascal, C,Haskell, …)

Dynamically typedStrongly typed and type checking is performed at runtime (LISP, Smalltalk,Python, …)

In some statically typed languages (e.g., ML), the programmer does not specifytypes at all. They are inferred by the compiler.

5/41

Page 12: Data Types & Memory Management - Principles of Programming ...nzeh/Teaching/3136/Slides/data-types.pdf · MEMORYLAYOUTOFRECORDS Aligned(fixedordering): name atomic_number atomic_weight

COMMON KINDS OF TYPE SYSTEMS

Strongly typedProhibits the application of an operation to any object not supporting thisoperation.

Statically typedStrongly typed and type checking is performed at compile time (Pascal, C,Haskell, …)

Dynamically typedStrongly typed and type checking is performed at runtime (LISP, Smalltalk,Python, …)

In some statically typed languages (e.g., ML), the programmer does not specifytypes at all. They are inferred by the compiler.

5/41

Page 13: Data Types & Memory Management - Principles of Programming ...nzeh/Teaching/3136/Slides/data-types.pdf · MEMORYLAYOUTOFRECORDS Aligned(fixedordering): name atomic_number atomic_weight

DEFINITION OF TYPES

Similar to subroutines in many languages, defining a type has two parts:

• A type’s declaration introduces its name into the current scope.• A type’s definition describes the type (the simpler types it is composed of).

Classification of types:

• Denotational: A type is a set of values.• Constructive: A type is built-in or composite.• Abstration-based: A type is defined by an interface, the set of operations itsupports.

6/41

Page 14: Data Types & Memory Management - Principles of Programming ...nzeh/Teaching/3136/Slides/data-types.pdf · MEMORYLAYOUTOFRECORDS Aligned(fixedordering): name atomic_number atomic_weight

DEFINITION OF TYPES

Similar to subroutines in many languages, defining a type has two parts:

• A type’s declaration introduces its name into the current scope.• A type’s definition describes the type (the simpler types it is composed of).

Classification of types:

• Denotational: A type is a set of values.• Constructive: A type is built-in or composite.• Abstration-based: A type is defined by an interface, the set of operations itsupports.

6/41

Page 15: Data Types & Memory Management - Principles of Programming ...nzeh/Teaching/3136/Slides/data-types.pdf · MEMORYLAYOUTOFRECORDS Aligned(fixedordering): name atomic_number atomic_weight

DEFINITION OF TYPES

Similar to subroutines in many languages, defining a type has two parts:

• A type’s declaration introduces its name into the current scope.• A type’s definition describes the type (the simpler types it is composed of).

Classification of types:

• Denotational: A type is a set of values.

• Constructive: A type is built-in or composite.• Abstration-based: A type is defined by an interface, the set of operations itsupports.

6/41

Page 16: Data Types & Memory Management - Principles of Programming ...nzeh/Teaching/3136/Slides/data-types.pdf · MEMORYLAYOUTOFRECORDS Aligned(fixedordering): name atomic_number atomic_weight

DEFINITION OF TYPES

Similar to subroutines in many languages, defining a type has two parts:

• A type’s declaration introduces its name into the current scope.• A type’s definition describes the type (the simpler types it is composed of).

Classification of types:

• Denotational: A type is a set of values.• Constructive: A type is built-in or composite.

• Abstration-based: A type is defined by an interface, the set of operations itsupports.

6/41

Page 17: Data Types & Memory Management - Principles of Programming ...nzeh/Teaching/3136/Slides/data-types.pdf · MEMORYLAYOUTOFRECORDS Aligned(fixedordering): name atomic_number atomic_weight

DEFINITION OF TYPES

Similar to subroutines in many languages, defining a type has two parts:

• A type’s declaration introduces its name into the current scope.• A type’s definition describes the type (the simpler types it is composed of).

Classification of types:

• Denotational: A type is a set of values.• Constructive: A type is built-in or composite.• Abstration-based: A type is defined by an interface, the set of operations itsupports.

6/41

Page 18: Data Types & Memory Management - Principles of Programming ...nzeh/Teaching/3136/Slides/data-types.pdf · MEMORYLAYOUTOFRECORDS Aligned(fixedordering): name atomic_number atomic_weight

CONSTRUCTIVE CLASSIFICATION OF TYPES

Built-in types: Integers, Booleans, characters, “real” numbers, …

Enumeration and range types: (Neither built-in nor composite)

• C: enum t { A, B };• Pascal: 0..100

Composite types: Records, arrays, files, lists, sets, pointers, …

7/41

Page 19: Data Types & Memory Management - Principles of Programming ...nzeh/Teaching/3136/Slides/data-types.pdf · MEMORYLAYOUTOFRECORDS Aligned(fixedordering): name atomic_number atomic_weight

CONSTRUCTIVE CLASSIFICATION OF TYPES

Built-in types: Integers, Booleans, characters, “real” numbers, …

Enumeration and range types: (Neither built-in nor composite)

• C: enum t { A, B };• Pascal: 0..100

Composite types: Records, arrays, files, lists, sets, pointers, …

7/41

Page 20: Data Types & Memory Management - Principles of Programming ...nzeh/Teaching/3136/Slides/data-types.pdf · MEMORYLAYOUTOFRECORDS Aligned(fixedordering): name atomic_number atomic_weight

CONSTRUCTIVE CLASSIFICATION OF TYPES

Built-in types: Integers, Booleans, characters, “real” numbers, …

Enumeration and range types: (Neither built-in nor composite)

• C: enum t { A, B };• Pascal: 0..100

Composite types: Records, arrays, files, lists, sets, pointers, …

7/41

Page 21: Data Types & Memory Management - Principles of Programming ...nzeh/Teaching/3136/Slides/data-types.pdf · MEMORYLAYOUTOFRECORDS Aligned(fixedordering): name atomic_number atomic_weight

ROAD MAP

• Type Systems• Records• Arrays• Associative arrays• Pointers• Memory management• Garbage collection

8/41

Page 22: Data Types & Memory Management - Principles of Programming ...nzeh/Teaching/3136/Slides/data-types.pdf · MEMORYLAYOUTOFRECORDS Aligned(fixedordering): name atomic_number atomic_weight

ROAD MAP

• Type Systems• Records• Arrays• Associative arrays• Pointers• Memory management• Garbage collection

8/41

Page 23: Data Types & Memory Management - Principles of Programming ...nzeh/Teaching/3136/Slides/data-types.pdf · MEMORYLAYOUTOFRECORDS Aligned(fixedordering): name atomic_number atomic_weight

RECORDS

A nested record definition in Pascal:

type ore = recordname : short_string;element_yielded : record

name : two_chars;atomic_n : integers;atomic_weight : real;metallic : Boolean

endend;

Accessing fields:

• ore.element_yielded.name• name of element_yielded of ore

9/41

Page 24: Data Types & Memory Management - Principles of Programming ...nzeh/Teaching/3136/Slides/data-types.pdf · MEMORYLAYOUTOFRECORDS Aligned(fixedordering): name atomic_number atomic_weight

RECORDS

A nested record definition in Pascal:

type ore = recordname : short_string;element_yielded : record

name : two_chars;atomic_n : integers;atomic_weight : real;metallic : Boolean

endend;

Accessing fields:

• ore.element_yielded.name• name of element_yielded of ore

9/41

Page 25: Data Types & Memory Management - Principles of Programming ...nzeh/Teaching/3136/Slides/data-types.pdf · MEMORYLAYOUTOFRECORDS Aligned(fixedordering): name atomic_number atomic_weight

MEMORY LAYOUT OF RECORDS

Aligned (fixed ordering):name

atomic_number

atomic_weight

metallic

− Potential waste of space+ One instruction per element access+ Guaranteed layout in memory(Good for systems programming)

Packed:name atomic_number

atomic_weightmetallic

+ No waste of space− Multiple instructions per element access+ Guaranteed layout in memory(Good for systems programming)

Aligned (optimized ordering):name

atomic_number

atomic_weight

metallic± Reduced space overhead+ One instruction per element access− No guarantee of layout in memory(Bad for systems programming)

10/41

Page 26: Data Types & Memory Management - Principles of Programming ...nzeh/Teaching/3136/Slides/data-types.pdf · MEMORYLAYOUTOFRECORDS Aligned(fixedordering): name atomic_number atomic_weight

MEMORY LAYOUT OF RECORDS

Aligned (fixed ordering):name

atomic_number

atomic_weight

metallic

− Potential waste of space+ One instruction per element access+ Guaranteed layout in memory(Good for systems programming)

Packed:name atomic_number

atomic_weightmetallic

+ No waste of space− Multiple instructions per element access+ Guaranteed layout in memory(Good for systems programming)

Aligned (optimized ordering):name

atomic_number

atomic_weight

metallic± Reduced space overhead+ One instruction per element access− No guarantee of layout in memory(Bad for systems programming)

10/41

Page 27: Data Types & Memory Management - Principles of Programming ...nzeh/Teaching/3136/Slides/data-types.pdf · MEMORYLAYOUTOFRECORDS Aligned(fixedordering): name atomic_number atomic_weight

MEMORY LAYOUT OF RECORDS

Aligned (fixed ordering):name

atomic_number

atomic_weight

metallic

− Potential waste of space+ One instruction per element access+ Guaranteed layout in memory(Good for systems programming)

Packed:name atomic_number

atomic_weightmetallic

+ No waste of space− Multiple instructions per element access+ Guaranteed layout in memory(Good for systems programming)

Aligned (optimized ordering):name

atomic_number

atomic_weight

metallic± Reduced space overhead+ One instruction per element access− No guarantee of layout in memory(Bad for systems programming)

10/41

Page 28: Data Types & Memory Management - Principles of Programming ...nzeh/Teaching/3136/Slides/data-types.pdf · MEMORYLAYOUTOFRECORDS Aligned(fixedordering): name atomic_number atomic_weight

MEMORY LAYOUT OF RECORDS

Aligned (fixed ordering):name

atomic_number

atomic_weight

metallic

− Potential waste of space+ One instruction per element access+ Guaranteed layout in memory(Good for systems programming)

Packed:name atomic_number

atomic_weightmetallic

+ No waste of space− Multiple instructions per element access+ Guaranteed layout in memory(Good for systems programming)

Aligned (optimized ordering):name

atomic_number

atomic_weight

metallic± Reduced space overhead+ One instruction per element access− No guarantee of layout in memory(Bad for systems programming)

10/41

Page 29: Data Types & Memory Management - Principles of Programming ...nzeh/Teaching/3136/Slides/data-types.pdf · MEMORYLAYOUTOFRECORDS Aligned(fixedordering): name atomic_number atomic_weight

MEMORY LAYOUT OF RECORDS

Aligned (fixed ordering):name

atomic_number

atomic_weight

metallic

− Potential waste of space+ One instruction per element access+ Guaranteed layout in memory(Good for systems programming)

Packed:name atomic_number

atomic_weightmetallic

+ No waste of space− Multiple instructions per element access+ Guaranteed layout in memory(Good for systems programming)

Aligned (optimized ordering):name

atomic_number

atomic_weight

metallic± Reduced space overhead+ One instruction per element access− No guarantee of layout in memory(Bad for systems programming)

10/41

Page 30: Data Types & Memory Management - Principles of Programming ...nzeh/Teaching/3136/Slides/data-types.pdf · MEMORYLAYOUTOFRECORDS Aligned(fixedordering): name atomic_number atomic_weight

MEMORY LAYOUT OF RECORDS

Aligned (fixed ordering):name

atomic_number

atomic_weight

metallic

− Potential waste of space+ One instruction per element access+ Guaranteed layout in memory(Good for systems programming)

Packed:name atomic_number

atomic_weightmetallic

+ No waste of space− Multiple instructions per element access+ Guaranteed layout in memory(Good for systems programming)

Aligned (optimized ordering):name

atomic_number

atomic_weight

metallic

± Reduced space overhead+ One instruction per element access− No guarantee of layout in memory(Bad for systems programming)

10/41

Page 31: Data Types & Memory Management - Principles of Programming ...nzeh/Teaching/3136/Slides/data-types.pdf · MEMORYLAYOUTOFRECORDS Aligned(fixedordering): name atomic_number atomic_weight

MEMORY LAYOUT OF RECORDS

Aligned (fixed ordering):name

atomic_number

atomic_weight

metallic

− Potential waste of space+ One instruction per element access+ Guaranteed layout in memory(Good for systems programming)

Packed:name atomic_number

atomic_weightmetallic

+ No waste of space− Multiple instructions per element access+ Guaranteed layout in memory(Good for systems programming)

Aligned (optimized ordering):name

atomic_number

atomic_weight

metallic± Reduced space overhead+ One instruction per element access− No guarantee of layout in memory(Bad for systems programming) 10/41

Page 32: Data Types & Memory Management - Principles of Programming ...nzeh/Teaching/3136/Slides/data-types.pdf · MEMORYLAYOUTOFRECORDS Aligned(fixedordering): name atomic_number atomic_weight

ROAD MAP

• Type Systems• Records• Arrays• Associative arrays• Pointers• Memory management• Garbage collection

11/41

Page 33: Data Types & Memory Management - Principles of Programming ...nzeh/Teaching/3136/Slides/data-types.pdf · MEMORYLAYOUTOFRECORDS Aligned(fixedordering): name atomic_number atomic_weight

ROAD MAP

• Type Systems• Records• Arrays• Associative arrays• Pointers• Memory management• Garbage collection

11/41

Page 34: Data Types & Memory Management - Principles of Programming ...nzeh/Teaching/3136/Slides/data-types.pdf · MEMORYLAYOUTOFRECORDS Aligned(fixedordering): name atomic_number atomic_weight

ARRAYS

Shape fixed when?Stored where?Compile time Elaboration time Dynamic

Static address Static — —Stack Local Local —Heap Dynamic Dynamic Dynamic

Issues:

• Memory allocation• Bounds checks• Index calculations (higher-dimensional arrays)

12/41

Page 35: Data Types & Memory Management - Principles of Programming ...nzeh/Teaching/3136/Slides/data-types.pdf · MEMORYLAYOUTOFRECORDS Aligned(fixedordering): name atomic_number atomic_weight

ELABORATION-TIME SHAPE BINDING AND DOPE VECTORSStackframe

“Static” part

“Dynamic” part

Efficient access to stack-allocated objects is based onevery element having a fixed offset in the stack frame.

How can we achieve this when we allowstack-allocated objects (e.g., arrays) to have sizesdetermined at elaboration time?

How do we allow index calculation and boundschecking for such arrays (and heap-allocated arrays)?

Dope vector:

Range Dimension 3Range Dimension 2Range Dimension 1Pointer Location

13/41

Page 36: Data Types & Memory Management - Principles of Programming ...nzeh/Teaching/3136/Slides/data-types.pdf · MEMORYLAYOUTOFRECORDS Aligned(fixedordering): name atomic_number atomic_weight

ELABORATION-TIME SHAPE BINDING AND DOPE VECTORSStackframe

“Static” part

“Dynamic” part

Efficient access to stack-allocated objects is based onevery element having a fixed offset in the stack frame.

How can we achieve this when we allowstack-allocated objects (e.g., arrays) to have sizesdetermined at elaboration time?

How do we allow index calculation and boundschecking for such arrays (and heap-allocated arrays)?

Dope vector:

Range Dimension 3Range Dimension 2Range Dimension 1Pointer Location

13/41

Page 37: Data Types & Memory Management - Principles of Programming ...nzeh/Teaching/3136/Slides/data-types.pdf · MEMORYLAYOUTOFRECORDS Aligned(fixedordering): name atomic_number atomic_weight

ELABORATION-TIME SHAPE BINDING AND DOPE VECTORSStackframe

“Static” part

“Dynamic” part

Efficient access to stack-allocated objects is based onevery element having a fixed offset in the stack frame.

How can we achieve this when we allowstack-allocated objects (e.g., arrays) to have sizesdetermined at elaboration time?

How do we allow index calculation and boundschecking for such arrays (and heap-allocated arrays)?

Dope vector:

Range Dimension 3Range Dimension 2Range Dimension 1Pointer Location

13/41

Page 38: Data Types & Memory Management - Principles of Programming ...nzeh/Teaching/3136/Slides/data-types.pdf · MEMORYLAYOUTOFRECORDS Aligned(fixedordering): name atomic_number atomic_weight

ELABORATION-TIME SHAPE BINDING AND DOPE VECTORSStackframe

“Static” part

“Dynamic” part

Efficient access to stack-allocated objects is based onevery element having a fixed offset in the stack frame.

How can we achieve this when we allowstack-allocated objects (e.g., arrays) to have sizesdetermined at elaboration time?

How do we allow index calculation and boundschecking for such arrays (and heap-allocated arrays)?

Dope vector:

Range Dimension 3Range Dimension 2Range Dimension 1Pointer Location

13/41

Page 39: Data Types & Memory Management - Principles of Programming ...nzeh/Teaching/3136/Slides/data-types.pdf · MEMORYLAYOUTOFRECORDS Aligned(fixedordering): name atomic_number atomic_weight

ELABORATION-TIME SHAPE BINDING AND DOPE VECTORSStackframe

“Static” part

“Dynamic” part

Efficient access to stack-allocated objects is based onevery element having a fixed offset in the stack frame.

How can we achieve this when we allowstack-allocated objects (e.g., arrays) to have sizesdetermined at elaboration time?

How do we allow index calculation and boundschecking for such arrays (and heap-allocated arrays)?

Dope vector:

Range Dimension 3Range Dimension 2Range Dimension 1Pointer Location

13/41

Page 40: Data Types & Memory Management - Principles of Programming ...nzeh/Teaching/3136/Slides/data-types.pdf · MEMORYLAYOUTOFRECORDS Aligned(fixedordering): name atomic_number atomic_weight

CONTIGUOUS MEMORY LAYOUT OF 2-D ARRAYS

Row-major layout Column-major layout

There are more sophisticated block-recursive layouts which, combined with theright algorithms, achieve much better cache efficiency than the above.

14/41

Page 41: Data Types & Memory Management - Principles of Programming ...nzeh/Teaching/3136/Slides/data-types.pdf · MEMORYLAYOUTOFRECORDS Aligned(fixedordering): name atomic_number atomic_weight

CONTIGUOUS MEMORY LAYOUT OF 2-D ARRAYS

Row-major layout Column-major layout

There are more sophisticated block-recursive layouts which, combined with theright algorithms, achieve much better cache efficiency than the above.

14/41

Page 42: Data Types & Memory Management - Principles of Programming ...nzeh/Teaching/3136/Slides/data-types.pdf · MEMORYLAYOUTOFRECORDS Aligned(fixedordering): name atomic_number atomic_weight

CONTIGUOUS VS ROW-POINTER LAYOUT

char days[][10] = {"Monday", "Tuesday", "Wednesday","Thursday", "Friday", "Saturday","Sunday"

};days[2][3] == 's';

S u n d a y \0S a t u r d a y \0F r i d a y \0T h u r s d a y \0W e d n e s d a y \0T u e s d a y \0M o n d a y \0

char *days[] = {"Monday", "Tuesday", "Wednesday","Thursday", "Friday", "Saturday","Sunday"

};days[2][3] == 's';

M o n d a y \0 T u es d a y \0 W e d n es d a y \0 T h u r sd a y \0 F r i d a y\0 S a t u r d a y \0S u n d a y \0

15/41

Page 43: Data Types & Memory Management - Principles of Programming ...nzeh/Teaching/3136/Slides/data-types.pdf · MEMORYLAYOUTOFRECORDS Aligned(fixedordering): name atomic_number atomic_weight

CONTIGUOUS VS ROW-POINTER LAYOUT

char days[][10] = {"Monday", "Tuesday", "Wednesday","Thursday", "Friday", "Saturday","Sunday"

};days[2][3] == 's';

S u n d a y \0S a t u r d a y \0F r i d a y \0T h u r s d a y \0W e d n e s d a y \0T u e s d a y \0M o n d a y \0

char *days[] = {"Monday", "Tuesday", "Wednesday","Thursday", "Friday", "Saturday","Sunday"

};days[2][3] == 's';

M o n d a y \0 T u es d a y \0 W e d n es d a y \0 T h u r sd a y \0 F r i d a y\0 S a t u r d a y \0S u n d a y \0

15/41

Page 44: Data Types & Memory Management - Principles of Programming ...nzeh/Teaching/3136/Slides/data-types.pdf · MEMORYLAYOUTOFRECORDS Aligned(fixedordering): name atomic_number atomic_weight

ROAD MAP

• Type Systems• Records• Arrays• Associative arrays• Pointers• Memory management• Garbage collection

16/41

Page 45: Data Types & Memory Management - Principles of Programming ...nzeh/Teaching/3136/Slides/data-types.pdf · MEMORYLAYOUTOFRECORDS Aligned(fixedordering): name atomic_number atomic_weight

ROAD MAP

• Type Systems• Records• Arrays• Associative arrays• Pointers• Memory management• Garbage collection

16/41

Page 46: Data Types & Memory Management - Principles of Programming ...nzeh/Teaching/3136/Slides/data-types.pdf · MEMORYLAYOUTOFRECORDS Aligned(fixedordering): name atomic_number atomic_weight

ASSOCIATIVE ARRAYS

• Allow arbitrary elements as indexes.• Directly supported in Perl and many other scripting languages.• C++ and Java call them maps (because they’re not really arrays!)• Scheme calls them association lists (A-lists).

Example:After (define e '((a 1) (b 2) (c 3))), (assoc 'a e) returns (a 1).

Different lookup operations for different notions of equality:

• assq uses eq? (identity)• assoc uses equal? (same value)• assv uses eqv? (halfway in between)

17/41

Page 47: Data Types & Memory Management - Principles of Programming ...nzeh/Teaching/3136/Slides/data-types.pdf · MEMORYLAYOUTOFRECORDS Aligned(fixedordering): name atomic_number atomic_weight

ASSOCIATIVE ARRAYS

• Allow arbitrary elements as indexes.• Directly supported in Perl and many other scripting languages.• C++ and Java call them maps (because they’re not really arrays!)• Scheme calls them association lists (A-lists).

Example:After (define e '((a 1) (b 2) (c 3))), (assoc 'a e) returns (a 1).

Different lookup operations for different notions of equality:

• assq uses eq? (identity)• assoc uses equal? (same value)• assv uses eqv? (halfway in between)

17/41

Page 48: Data Types & Memory Management - Principles of Programming ...nzeh/Teaching/3136/Slides/data-types.pdf · MEMORYLAYOUTOFRECORDS Aligned(fixedordering): name atomic_number atomic_weight

ASSOCIATIVE ARRAYS

• Allow arbitrary elements as indexes.• Directly supported in Perl and many other scripting languages.• C++ and Java call them maps (because they’re not really arrays!)• Scheme calls them association lists (A-lists).

Example:After (define e '((a 1) (b 2) (c 3))), (assoc 'a e) returns (a 1).

Different lookup operations for different notions of equality:

• assq uses eq? (identity)• assoc uses equal? (same value)• assv uses eqv? (halfway in between)

17/41

Page 49: Data Types & Memory Management - Principles of Programming ...nzeh/Teaching/3136/Slides/data-types.pdf · MEMORYLAYOUTOFRECORDS Aligned(fixedordering): name atomic_number atomic_weight

ARRAYS, LISTS, AND STRINGS

Most imperative languages provide excellent built-in support for arraymanipulation but not for operations on lists.

Most functional languages provide excellent built-in support for list manipulationbut not for operations on arrays.

Arrays are a natural way to store sequences when manipulating individualelements in place (i.e., imperatively).

Functional arrays that allow updates without copying the entire array areseriously non-trivial to implement.

Lists are naturally recursive and thus fit extremely well into the recursiveapproach taken to most problems in functional programming.

Strings are arrays of characters in imperative languages and lists of characters infunctional languages.

18/41

Page 50: Data Types & Memory Management - Principles of Programming ...nzeh/Teaching/3136/Slides/data-types.pdf · MEMORYLAYOUTOFRECORDS Aligned(fixedordering): name atomic_number atomic_weight

ARRAYS, LISTS, AND STRINGS

Most imperative languages provide excellent built-in support for arraymanipulation but not for operations on lists.

Most functional languages provide excellent built-in support for list manipulationbut not for operations on arrays.

Arrays are a natural way to store sequences when manipulating individualelements in place (i.e., imperatively).

Functional arrays that allow updates without copying the entire array areseriously non-trivial to implement.

Lists are naturally recursive and thus fit extremely well into the recursiveapproach taken to most problems in functional programming.

Strings are arrays of characters in imperative languages and lists of characters infunctional languages.

18/41

Page 51: Data Types & Memory Management - Principles of Programming ...nzeh/Teaching/3136/Slides/data-types.pdf · MEMORYLAYOUTOFRECORDS Aligned(fixedordering): name atomic_number atomic_weight

ARRAYS, LISTS, AND STRINGS

Most imperative languages provide excellent built-in support for arraymanipulation but not for operations on lists.

Most functional languages provide excellent built-in support for list manipulationbut not for operations on arrays.

Arrays are a natural way to store sequences when manipulating individualelements in place (i.e., imperatively).

Functional arrays that allow updates without copying the entire array areseriously non-trivial to implement.

Lists are naturally recursive and thus fit extremely well into the recursiveapproach taken to most problems in functional programming.

Strings are arrays of characters in imperative languages and lists of characters infunctional languages.

18/41

Page 52: Data Types & Memory Management - Principles of Programming ...nzeh/Teaching/3136/Slides/data-types.pdf · MEMORYLAYOUTOFRECORDS Aligned(fixedordering): name atomic_number atomic_weight

ARRAYS, LISTS, AND STRINGS

Most imperative languages provide excellent built-in support for arraymanipulation but not for operations on lists.

Most functional languages provide excellent built-in support for list manipulationbut not for operations on arrays.

Arrays are a natural way to store sequences when manipulating individualelements in place (i.e., imperatively).

Functional arrays that allow updates without copying the entire array areseriously non-trivial to implement.

Lists are naturally recursive and thus fit extremely well into the recursiveapproach taken to most problems in functional programming.

Strings are arrays of characters in imperative languages and lists of characters infunctional languages.

18/41

Page 53: Data Types & Memory Management - Principles of Programming ...nzeh/Teaching/3136/Slides/data-types.pdf · MEMORYLAYOUTOFRECORDS Aligned(fixedordering): name atomic_number atomic_weight

ARRAYS, LISTS, AND STRINGS

Most imperative languages provide excellent built-in support for arraymanipulation but not for operations on lists.

Most functional languages provide excellent built-in support for list manipulationbut not for operations on arrays.

Arrays are a natural way to store sequences when manipulating individualelements in place (i.e., imperatively).

Functional arrays that allow updates without copying the entire array areseriously non-trivial to implement.

Lists are naturally recursive and thus fit extremely well into the recursiveapproach taken to most problems in functional programming.

Strings are arrays of characters in imperative languages and lists of characters infunctional languages.

18/41

Page 54: Data Types & Memory Management - Principles of Programming ...nzeh/Teaching/3136/Slides/data-types.pdf · MEMORYLAYOUTOFRECORDS Aligned(fixedordering): name atomic_number atomic_weight

ROAD MAP

• Type Systems• Records• Arrays• Associative arrays• Pointers• Memory management• Garbage collection

19/41

Page 55: Data Types & Memory Management - Principles of Programming ...nzeh/Teaching/3136/Slides/data-types.pdf · MEMORYLAYOUTOFRECORDS Aligned(fixedordering): name atomic_number atomic_weight

ROAD MAP

• Type Systems• Records• Arrays• Associative arrays• Pointers• Memory management• Garbage collection

19/41

Page 56: Data Types & Memory Management - Principles of Programming ...nzeh/Teaching/3136/Slides/data-types.pdf · MEMORYLAYOUTOFRECORDS Aligned(fixedordering): name atomic_number atomic_weight

POINTERS

• Point to memory locations that store data (often typed, e.g., int*).• Not needed when using a reference model of variables (Lisp, ML, Clu, Java).• Needed for recursive types when using a value model of variables (C, Pascal,Ada).

Storage reclamation:

• Explicit (manual)• Automatic (garbage collection)

Advantages and disadvantages of explicit storage reclamation:

+ Garbage collection can incur serious run-time overhead.− Potential for memory leaks.− Potential for dangling pointers and segmentation faults.

20/41

Page 57: Data Types & Memory Management - Principles of Programming ...nzeh/Teaching/3136/Slides/data-types.pdf · MEMORYLAYOUTOFRECORDS Aligned(fixedordering): name atomic_number atomic_weight

POINTERS

• Point to memory locations that store data (often typed, e.g., int*).• Not needed when using a reference model of variables (Lisp, ML, Clu, Java).• Needed for recursive types when using a value model of variables (C, Pascal,Ada).

Storage reclamation:

• Explicit (manual)• Automatic (garbage collection)

Advantages and disadvantages of explicit storage reclamation:

+ Garbage collection can incur serious run-time overhead.− Potential for memory leaks.− Potential for dangling pointers and segmentation faults.

20/41

Page 58: Data Types & Memory Management - Principles of Programming ...nzeh/Teaching/3136/Slides/data-types.pdf · MEMORYLAYOUTOFRECORDS Aligned(fixedordering): name atomic_number atomic_weight

POINTERS

• Point to memory locations that store data (often typed, e.g., int*).• Not needed when using a reference model of variables (Lisp, ML, Clu, Java).• Needed for recursive types when using a value model of variables (C, Pascal,Ada).

Storage reclamation:

• Explicit (manual)• Automatic (garbage collection)

Advantages and disadvantages of explicit storage reclamation:

+ Garbage collection can incur serious run-time overhead.− Potential for memory leaks.− Potential for dangling pointers and segmentation faults.

20/41

Page 59: Data Types & Memory Management - Principles of Programming ...nzeh/Teaching/3136/Slides/data-types.pdf · MEMORYLAYOUTOFRECORDS Aligned(fixedordering): name atomic_number atomic_weight

POINTER ALLOCATION AND DEALLOCATION

C:

• p = (element *)malloc(sizeof(element))• free(p)• Not type-safe, explicit deallocation

Pascal:

• new(p)• dispose(p)• Type-safe, explicit deallocation

Java/C++:

• p = new element() (semantics different between Java and C++, how?)• delete p (in C++)• Type-safe, explicit deallocation in C++, garbage collection in Java

21/41

Page 60: Data Types & Memory Management - Principles of Programming ...nzeh/Teaching/3136/Slides/data-types.pdf · MEMORYLAYOUTOFRECORDS Aligned(fixedordering): name atomic_number atomic_weight

ROAD MAP

• Type Systems• Records• Arrays• Associative arrays• Pointers• Memory management• Garbage collection

22/41

Page 61: Data Types & Memory Management - Principles of Programming ...nzeh/Teaching/3136/Slides/data-types.pdf · MEMORYLAYOUTOFRECORDS Aligned(fixedordering): name atomic_number atomic_weight

ROAD MAP

• Type Systems• Records• Arrays• Associative arrays• Pointers• Memory management• Garbage collection

22/41

Page 62: Data Types & Memory Management - Principles of Programming ...nzeh/Teaching/3136/Slides/data-types.pdf · MEMORYLAYOUTOFRECORDS Aligned(fixedordering): name atomic_number atomic_weight

DANGLING REFERENCES

Dangling referenceA pointer to an already reclaimed object

Can only happen when reclamation of objects is the responsibility of theprogrammer.

Dangling references are notoriously hard to debug and a major source ofprogram misbehaviour and security holes.

Techniques to catch them:

• Tombstones• Keys and locks

Idea: Better crash and expose the bug than do the wrong thing.

23/41

Page 63: Data Types & Memory Management - Principles of Programming ...nzeh/Teaching/3136/Slides/data-types.pdf · MEMORYLAYOUTOFRECORDS Aligned(fixedordering): name atomic_number atomic_weight

DANGLING REFERENCES

Dangling referenceA pointer to an already reclaimed object

Can only happen when reclamation of objects is the responsibility of theprogrammer.

Dangling references are notoriously hard to debug and a major source ofprogram misbehaviour and security holes.

Techniques to catch them:

• Tombstones• Keys and locks

Idea: Better crash and expose the bug than do the wrong thing.

23/41

Page 64: Data Types & Memory Management - Principles of Programming ...nzeh/Teaching/3136/Slides/data-types.pdf · MEMORYLAYOUTOFRECORDS Aligned(fixedordering): name atomic_number atomic_weight

DANGLING REFERENCES

Dangling referenceA pointer to an already reclaimed object

Can only happen when reclamation of objects is the responsibility of theprogrammer.

Dangling references are notoriously hard to debug and a major source ofprogram misbehaviour and security holes.

Techniques to catch them:

• Tombstones• Keys and locks

Idea: Better crash and expose the bug than do the wrong thing.

23/41

Page 65: Data Types & Memory Management - Principles of Programming ...nzeh/Teaching/3136/Slides/data-types.pdf · MEMORYLAYOUTOFRECORDS Aligned(fixedordering): name atomic_number atomic_weight

DANGLING REFERENCES

Dangling referenceA pointer to an already reclaimed object

Can only happen when reclamation of objects is the responsibility of theprogrammer.

Dangling references are notoriously hard to debug and a major source ofprogram misbehaviour and security holes.

Techniques to catch them:

• Tombstones• Keys and locks

Idea: Better crash and expose the bug than do the wrong thing.23/41

Page 66: Data Types & Memory Management - Principles of Programming ...nzeh/Teaching/3136/Slides/data-types.pdf · MEMORYLAYOUTOFRECORDS Aligned(fixedordering): name atomic_number atomic_weight

TOMBSTONES (1)

new(p) p:

q := p p:q:

delete(p) p:q:

RIPnull

24/41

Page 67: Data Types & Memory Management - Principles of Programming ...nzeh/Teaching/3136/Slides/data-types.pdf · MEMORYLAYOUTOFRECORDS Aligned(fixedordering): name atomic_number atomic_weight

TOMBSTONES (2)

Issues:

• Space overhead• Runtime overhead (two cache misses instead of one)• Check for invalid tombstones = hardware interrupt (cheap):

• RIP = null pointer• How to allocate the tombstones?

• From separate heap (no fragmentation)• Need reference count or other garbage collection strategy to determine when Ican delete a tombstone.

• Need to track pointers to objects on the stack, in order to invalidate theirtombstones.

Interesting side effect:

• The extra level of indirection allows us to compact memory.

25/41

Page 68: Data Types & Memory Management - Principles of Programming ...nzeh/Teaching/3136/Slides/data-types.pdf · MEMORYLAYOUTOFRECORDS Aligned(fixedordering): name atomic_number atomic_weight

TOMBSTONES (2)

Issues:

• Space overhead• Runtime overhead (two cache misses instead of one)• Check for invalid tombstones = hardware interrupt (cheap):

• RIP = null pointer• How to allocate the tombstones?

• From separate heap (no fragmentation)• Need reference count or other garbage collection strategy to determine when Ican delete a tombstone.

• Need to track pointers to objects on the stack, in order to invalidate theirtombstones.

Interesting side effect:

• The extra level of indirection allows us to compact memory.25/41

Page 69: Data Types & Memory Management - Principles of Programming ...nzeh/Teaching/3136/Slides/data-types.pdf · MEMORYLAYOUTOFRECORDS Aligned(fixedordering): name atomic_number atomic_weight

LOCKS AND KEYS (1)

q := p p: 23642364

q := p p:q:

2364

2364

2364

q := p p:q:

0000

2364

0000null

26/41

Page 70: Data Types & Memory Management - Principles of Programming ...nzeh/Teaching/3136/Slides/data-types.pdf · MEMORYLAYOUTOFRECORDS Aligned(fixedordering): name atomic_number atomic_weight

LOCKS AND KEYS (2)

Comparison to tombstones:

• Can only be used for heap-allocated objects• Provides only probabilistic protection• Unclear which one has higher runtime overhead• Unclear which one has higher space overhead

Languages that provide for such checks for dangling references often allow themto be turned on/off using compile-time flags.

27/41

Page 71: Data Types & Memory Management - Principles of Programming ...nzeh/Teaching/3136/Slides/data-types.pdf · MEMORYLAYOUTOFRECORDS Aligned(fixedordering): name atomic_number atomic_weight

LOCKS AND KEYS (2)

Comparison to tombstones:

• Can only be used for heap-allocated objects• Provides only probabilistic protection• Unclear which one has higher runtime overhead• Unclear which one has higher space overhead

Languages that provide for such checks for dangling references often allow themto be turned on/off using compile-time flags.

27/41

Page 72: Data Types & Memory Management - Principles of Programming ...nzeh/Teaching/3136/Slides/data-types.pdf · MEMORYLAYOUTOFRECORDS Aligned(fixedordering): name atomic_number atomic_weight

ROAD MAP

• Type Systems• Records• Arrays• Associative arrays• Pointers• Memory management• Garbage collection

28/41

Page 73: Data Types & Memory Management - Principles of Programming ...nzeh/Teaching/3136/Slides/data-types.pdf · MEMORYLAYOUTOFRECORDS Aligned(fixedordering): name atomic_number atomic_weight

ROAD MAP

• Type Systems• Records• Arrays• Associative arrays• Pointers• Memory management• Garbage collection

28/41

Page 74: Data Types & Memory Management - Principles of Programming ...nzeh/Teaching/3136/Slides/data-types.pdf · MEMORYLAYOUTOFRECORDS Aligned(fixedordering): name atomic_number atomic_weight

GARBAGE COLLECTION

Garbage collectionAutomatic reclamation of heap space

• Essential for functional languages• Popular in modern imperative languages (Java, Python, Ada, Clu, …)• Difficult to implement• Slower than manual reclamation

29/41

Page 75: Data Types & Memory Management - Principles of Programming ...nzeh/Teaching/3136/Slides/data-types.pdf · MEMORYLAYOUTOFRECORDS Aligned(fixedordering): name atomic_number atomic_weight

GARBAGE COLLECTION STRATEGIES

• Reference counts

• Mark and sweep

• Mark and sweep variants:

• Stop and copy• Generational GC

30/41

Page 76: Data Types & Memory Management - Principles of Programming ...nzeh/Teaching/3136/Slides/data-types.pdf · MEMORYLAYOUTOFRECORDS Aligned(fixedordering): name atomic_number atomic_weight

REFERENCE COUNTS (1)

a = new Obj();b = new Obj();b = a;a = null;b = null;

a:

b:

Reclaimed

Reclaimed

null

null

31/41

Page 77: Data Types & Memory Management - Principles of Programming ...nzeh/Teaching/3136/Slides/data-types.pdf · MEMORYLAYOUTOFRECORDS Aligned(fixedordering): name atomic_number atomic_weight

REFERENCE COUNTS (1)

a = new Obj();

b = new Obj();b = a;a = null;b = null;

a:

b:

Reclaimed

Reclaimed

null

null

31/41

Page 78: Data Types & Memory Management - Principles of Programming ...nzeh/Teaching/3136/Slides/data-types.pdf · MEMORYLAYOUTOFRECORDS Aligned(fixedordering): name atomic_number atomic_weight

REFERENCE COUNTS (1)

a = new Obj();

b = new Obj();b = a;a = null;b = null;

a:

b:

1

Reclaimed

Reclaimed

null

null

31/41

Page 79: Data Types & Memory Management - Principles of Programming ...nzeh/Teaching/3136/Slides/data-types.pdf · MEMORYLAYOUTOFRECORDS Aligned(fixedordering): name atomic_number atomic_weight

REFERENCE COUNTS (1)

a = new Obj();b = new Obj();

b = a;a = null;b = null;

a:

b:

1

Reclaimed

Reclaimed

null

null

31/41

Page 80: Data Types & Memory Management - Principles of Programming ...nzeh/Teaching/3136/Slides/data-types.pdf · MEMORYLAYOUTOFRECORDS Aligned(fixedordering): name atomic_number atomic_weight

REFERENCE COUNTS (1)

a = new Obj();b = new Obj();

b = a;a = null;b = null;

a:

b:

1

Reclaimed

1

Reclaimed

null

null

31/41

Page 81: Data Types & Memory Management - Principles of Programming ...nzeh/Teaching/3136/Slides/data-types.pdf · MEMORYLAYOUTOFRECORDS Aligned(fixedordering): name atomic_number atomic_weight

REFERENCE COUNTS (1)

a = new Obj();b = new Obj();b = a;

a = null;b = null;

a:

b:

1

Reclaimed

1

Reclaimed

null

null

31/41

Page 82: Data Types & Memory Management - Principles of Programming ...nzeh/Teaching/3136/Slides/data-types.pdf · MEMORYLAYOUTOFRECORDS Aligned(fixedordering): name atomic_number atomic_weight

REFERENCE COUNTS (1)

a = new Obj();b = new Obj();b = a;

a = null;b = null;

a:

b:

1

Reclaimed

1

Reclaimed

null

null

31/41

Page 83: Data Types & Memory Management - Principles of Programming ...nzeh/Teaching/3136/Slides/data-types.pdf · MEMORYLAYOUTOFRECORDS Aligned(fixedordering): name atomic_number atomic_weight

REFERENCE COUNTS (1)

a = new Obj();b = new Obj();b = a;

a = null;b = null;

a:

b:

1

Reclaimed

0

Reclaimed

null

null

31/41

Page 84: Data Types & Memory Management - Principles of Programming ...nzeh/Teaching/3136/Slides/data-types.pdf · MEMORYLAYOUTOFRECORDS Aligned(fixedordering): name atomic_number atomic_weight

REFERENCE COUNTS (1)

a = new Obj();b = new Obj();b = a;

a = null;b = null;

a:

b:

2

Reclaimed

0

Reclaimed

null

null

31/41

Page 85: Data Types & Memory Management - Principles of Programming ...nzeh/Teaching/3136/Slides/data-types.pdf · MEMORYLAYOUTOFRECORDS Aligned(fixedordering): name atomic_number atomic_weight

REFERENCE COUNTS (1)

a = new Obj();b = new Obj();b = a;

a = null;b = null;

a:

b:

2

Reclaimed

0

Reclaimed

null

null

31/41

Page 86: Data Types & Memory Management - Principles of Programming ...nzeh/Teaching/3136/Slides/data-types.pdf · MEMORYLAYOUTOFRECORDS Aligned(fixedordering): name atomic_number atomic_weight

REFERENCE COUNTS (1)

a = new Obj();b = new Obj();b = a;a = null;

b = null;

a:

b:

2

Reclaimed

0

Reclaimed

null

null

31/41

Page 87: Data Types & Memory Management - Principles of Programming ...nzeh/Teaching/3136/Slides/data-types.pdf · MEMORYLAYOUTOFRECORDS Aligned(fixedordering): name atomic_number atomic_weight

REFERENCE COUNTS (1)

a = new Obj();b = new Obj();b = a;a = null;

b = null;

a:

b:

2

Reclaimed

0

Reclaimed

null

null

31/41

Page 88: Data Types & Memory Management - Principles of Programming ...nzeh/Teaching/3136/Slides/data-types.pdf · MEMORYLAYOUTOFRECORDS Aligned(fixedordering): name atomic_number atomic_weight

REFERENCE COUNTS (1)

a = new Obj();b = new Obj();b = a;a = null;

b = null;

a:

b:

1

Reclaimed

0

Reclaimed

null

null

31/41

Page 89: Data Types & Memory Management - Principles of Programming ...nzeh/Teaching/3136/Slides/data-types.pdf · MEMORYLAYOUTOFRECORDS Aligned(fixedordering): name atomic_number atomic_weight

REFERENCE COUNTS (1)

a = new Obj();b = new Obj();b = a;a = null;b = null;

a:

b:

1

Reclaimed

0

Reclaimed

null

null

31/41

Page 90: Data Types & Memory Management - Principles of Programming ...nzeh/Teaching/3136/Slides/data-types.pdf · MEMORYLAYOUTOFRECORDS Aligned(fixedordering): name atomic_number atomic_weight

REFERENCE COUNTS (1)

a = new Obj();b = new Obj();b = a;a = null;b = null;

a:

b:

1

Reclaimed

0

Reclaimed

null

null

31/41

Page 91: Data Types & Memory Management - Principles of Programming ...nzeh/Teaching/3136/Slides/data-types.pdf · MEMORYLAYOUTOFRECORDS Aligned(fixedordering): name atomic_number atomic_weight

REFERENCE COUNTS (1)

a = new Obj();b = new Obj();b = a;a = null;b = null;

a:

b:

0

Reclaimed

0

Reclaimed

null

null

31/41

Page 92: Data Types & Memory Management - Principles of Programming ...nzeh/Teaching/3136/Slides/data-types.pdf · MEMORYLAYOUTOFRECORDS Aligned(fixedordering): name atomic_number atomic_weight

REFERENCE COUNTS (1)

a = new Obj();b = new Obj();b = a;a = null;b = null;

a:

b:

0

Reclaimed

0

Reclaimed

null

null

31/41

Page 93: Data Types & Memory Management - Principles of Programming ...nzeh/Teaching/3136/Slides/data-types.pdf · MEMORYLAYOUTOFRECORDS Aligned(fixedordering): name atomic_number atomic_weight

REFERENCE COUNTS (2)

Subroutine return:

• Decrease reference counts for all objects referenced by variables insubroutine’s stack frame.

• Requires us to keep track of which entries in a stack frame are pointers.

Pros/cons:

+ Fairly simple to implement+ Fairly low cost− Does not work when there are circular references. Why?

32/41

Page 94: Data Types & Memory Management - Principles of Programming ...nzeh/Teaching/3136/Slides/data-types.pdf · MEMORYLAYOUTOFRECORDS Aligned(fixedordering): name atomic_number atomic_weight

REFERENCE COUNTS (2)

Subroutine return:

• Decrease reference counts for all objects referenced by variables insubroutine’s stack frame.

• Requires us to keep track of which entries in a stack frame are pointers.

Pros/cons:

+ Fairly simple to implement+ Fairly low cost− Does not work when there are circular references. Why?

32/41

Page 95: Data Types & Memory Management - Principles of Programming ...nzeh/Teaching/3136/Slides/data-types.pdf · MEMORYLAYOUTOFRECORDS Aligned(fixedordering): name atomic_number atomic_weight

MARK AND SWEEP (1)

Mark and sweep algorithm

• Mark every allocated memory block as useless.• For every pointer in the static address space and on the stack, mark theblock it points to as useful.

• For every block whose status changes from useless to useful, mark theblocks referenced by pointers in this block as useful. Apply this rulerecursively.

• Reclaim all blocks marked as useless.

33/41

Page 96: Data Types & Memory Management - Principles of Programming ...nzeh/Teaching/3136/Slides/data-types.pdf · MEMORYLAYOUTOFRECORDS Aligned(fixedordering): name atomic_number atomic_weight

MARK AND SWEEP (2)

Pros/cons:

− More complicated to implement.− Requires inspection of all allocated blocks in a sweep: costly.− High space usage if the recursion is deep.− Requires type descriptor at the beginning of each block to know the size ofthe block and to find the pointers in the block.

+ Works with circular data structures.

34/41

Page 97: Data Types & Memory Management - Principles of Programming ...nzeh/Teaching/3136/Slides/data-types.pdf · MEMORYLAYOUTOFRECORDS Aligned(fixedordering): name atomic_number atomic_weight

MARK AND SWEEP (2)

Pros/cons:

− More complicated to implement.− Requires inspection of all allocated blocks in a sweep: costly.− High space usage if the recursion is deep.− Requires type descriptor at the beginning of each block to know the size ofthe block and to find the pointers in the block.

+ Works with circular data structures.

34/41

Page 98: Data Types & Memory Management - Principles of Programming ...nzeh/Teaching/3136/Slides/data-types.pdf · MEMORYLAYOUTOFRECORDS Aligned(fixedordering): name atomic_number atomic_weight

SPACE-EFFICIENT MARK AND SWEEP

null nullcurrback

back = null

null

currback currback

currback

null null

curr

null null

curr

null null

curr

W X

U V

S T

R

Space: 1+ ⌈log2 f⌉ bits per record (f = number of fields in record)

35/41

Page 99: Data Types & Memory Management - Principles of Programming ...nzeh/Teaching/3136/Slides/data-types.pdf · MEMORYLAYOUTOFRECORDS Aligned(fixedordering): name atomic_number atomic_weight

SPACE-EFFICIENT MARK AND SWEEP

null null

curr

back

back = null

null

currback currback

currback

null null

curr

null null

curr

null null

curr

W X

U V

S T

R

Space: 1+ ⌈log2 f⌉ bits per record (f = number of fields in record)

35/41

Page 100: Data Types & Memory Management - Principles of Programming ...nzeh/Teaching/3136/Slides/data-types.pdf · MEMORYLAYOUTOFRECORDS Aligned(fixedordering): name atomic_number atomic_weight

SPACE-EFFICIENT MARK AND SWEEP

null null

curr

back

back = null

null

currback currback

currback

null null

curr

null null

curr

null null

curr

W X

U V

S T

R

Space: 1+ ⌈log2 f⌉ bits per record (f = number of fields in record)

35/41

Page 101: Data Types & Memory Management - Principles of Programming ...nzeh/Teaching/3136/Slides/data-types.pdf · MEMORYLAYOUTOFRECORDS Aligned(fixedordering): name atomic_number atomic_weight

SPACE-EFFICIENT MARK AND SWEEP

null

nullcurr

back

back = null

nullcurr

back currback

currback

null null

curr

null null

curr

null null

curr

W X

U V

S T

R

Space: 1+ ⌈log2 f⌉ bits per record (f = number of fields in record)

35/41

Page 102: Data Types & Memory Management - Principles of Programming ...nzeh/Teaching/3136/Slides/data-types.pdf · MEMORYLAYOUTOFRECORDS Aligned(fixedordering): name atomic_number atomic_weight

SPACE-EFFICIENT MARK AND SWEEP

null

nullcurr

back

back = null

nullcurr

back currback

currback

null null

curr

null null

curr

null null

curr

W X

U V

S T

R

Space: 1+ ⌈log2 f⌉ bits per record (f = number of fields in record)

35/41

Page 103: Data Types & Memory Management - Principles of Programming ...nzeh/Teaching/3136/Slides/data-types.pdf · MEMORYLAYOUTOFRECORDS Aligned(fixedordering): name atomic_number atomic_weight

SPACE-EFFICIENT MARK AND SWEEP

null

nullcurrback

back = null

null

curr

back

currback

curr

back

null null

curr

null null

curr

null null

curr

W X

U V

S T

R

Space: 1+ ⌈log2 f⌉ bits per record (f = number of fields in record)

35/41

Page 104: Data Types & Memory Management - Principles of Programming ...nzeh/Teaching/3136/Slides/data-types.pdf · MEMORYLAYOUTOFRECORDS Aligned(fixedordering): name atomic_number atomic_weight

SPACE-EFFICIENT MARK AND SWEEP

null

nullcurrback

back = null

null

curr

back

currback

curr

back

null null

curr

null null

curr

null null

curr

W X

U V

S T

R

Space: 1+ ⌈log2 f⌉ bits per record (f = number of fields in record)

35/41

Page 105: Data Types & Memory Management - Principles of Programming ...nzeh/Teaching/3136/Slides/data-types.pdf · MEMORYLAYOUTOFRECORDS Aligned(fixedordering): name atomic_number atomic_weight

SPACE-EFFICIENT MARK AND SWEEP

null

nullcurrback

back = null

null

currback currback

curr

back null null

curr

null nullcurr null null

curr

W X

U V

S T

R

Space: 1+ ⌈log2 f⌉ bits per record (f = number of fields in record)

35/41

Page 106: Data Types & Memory Management - Principles of Programming ...nzeh/Teaching/3136/Slides/data-types.pdf · MEMORYLAYOUTOFRECORDS Aligned(fixedordering): name atomic_number atomic_weight

SPACE-EFFICIENT MARK AND SWEEP

null

nullcurrback

back = null

null

currback currback

curr

back null null

curr

null nullcurr null null

curr

W X

U V

S T

R

Space: 1+ ⌈log2 f⌉ bits per record (f = number of fields in record)

35/41

Page 107: Data Types & Memory Management - Principles of Programming ...nzeh/Teaching/3136/Slides/data-types.pdf · MEMORYLAYOUTOFRECORDS Aligned(fixedordering): name atomic_number atomic_weight

SPACE-EFFICIENT MARK AND SWEEP

null

nullcurrback

back = null

null

curr

back

currback

curr

back

null null

curr

null null

curr

null null

curr

W X

U V

S T

R

Space: 1+ ⌈log2 f⌉ bits per record (f = number of fields in record)

35/41

Page 108: Data Types & Memory Management - Principles of Programming ...nzeh/Teaching/3136/Slides/data-types.pdf · MEMORYLAYOUTOFRECORDS Aligned(fixedordering): name atomic_number atomic_weight

SPACE-EFFICIENT MARK AND SWEEP

null

nullcurrback

back = null

null

currback currback

curr

back null null

curr

null null

curr

null null currW X

U V

S T

R

Space: 1+ ⌈log2 f⌉ bits per record (f = number of fields in record)

35/41

Page 109: Data Types & Memory Management - Principles of Programming ...nzeh/Teaching/3136/Slides/data-types.pdf · MEMORYLAYOUTOFRECORDS Aligned(fixedordering): name atomic_number atomic_weight

SPACE-EFFICIENT MARK AND SWEEP

null

nullcurrback

back = null

null

currback currback

curr

back null null

curr

null null

curr

null null currW X

U V

S T

R

Space: 1+ ⌈log2 f⌉ bits per record (f = number of fields in record)

35/41

Page 110: Data Types & Memory Management - Principles of Programming ...nzeh/Teaching/3136/Slides/data-types.pdf · MEMORYLAYOUTOFRECORDS Aligned(fixedordering): name atomic_number atomic_weight

SPACE-EFFICIENT MARK AND SWEEP

null

nullcurrback

back = null

null

curr

back

currback

curr

back

null null

curr

null null

curr

null null

curr

W X

U V

S T

R

Space: 1+ ⌈log2 f⌉ bits per record (f = number of fields in record)

35/41

Page 111: Data Types & Memory Management - Principles of Programming ...nzeh/Teaching/3136/Slides/data-types.pdf · MEMORYLAYOUTOFRECORDS Aligned(fixedordering): name atomic_number atomic_weight

SPACE-EFFICIENT MARK AND SWEEP

null

nullcurr

back

back = null

nullcurr

back currback

currback

null null

curr

null null

curr

null null

curr

W X

U V

S T

R

Space: 1+ ⌈log2 f⌉ bits per record (f = number of fields in record)

35/41

Page 112: Data Types & Memory Management - Principles of Programming ...nzeh/Teaching/3136/Slides/data-types.pdf · MEMORYLAYOUTOFRECORDS Aligned(fixedordering): name atomic_number atomic_weight

SPACE-EFFICIENT MARK AND SWEEP

null null

curr

back

back = null

null

currback currback

currback

null null

curr

null null

curr

null null

curr

W X

U V

S T

R

Space: 1+ ⌈log2 f⌉ bits per record (f = number of fields in record)

35/41

Page 113: Data Types & Memory Management - Principles of Programming ...nzeh/Teaching/3136/Slides/data-types.pdf · MEMORYLAYOUTOFRECORDS Aligned(fixedordering): name atomic_number atomic_weight

SPACE-EFFICIENT MARK AND SWEEP

null

null

curr

back

back = null

null

currback

curr

back

currback

null null

curr

null null

curr

null null

curr

W X

U V

S T

R

Space: 1+ ⌈log2 f⌉ bits per record (f = number of fields in record)

35/41

Page 114: Data Types & Memory Management - Principles of Programming ...nzeh/Teaching/3136/Slides/data-types.pdf · MEMORYLAYOUTOFRECORDS Aligned(fixedordering): name atomic_number atomic_weight

SPACE-EFFICIENT MARK AND SWEEP

null

null

curr

back

back = null

null

currback

curr

back

currback

null null

curr

null null

curr

null null

curr

W X

U V

S T

R

Space: 1+ ⌈log2 f⌉ bits per record (f = number of fields in record)

35/41

Page 115: Data Types & Memory Management - Principles of Programming ...nzeh/Teaching/3136/Slides/data-types.pdf · MEMORYLAYOUTOFRECORDS Aligned(fixedordering): name atomic_number atomic_weight

SPACE-EFFICIENT MARK AND SWEEP

null

null

currback

back = null

null

currback curr

back

curr

back

null null

curr

null null

curr

null null

curr

W X

U V

S T

R

Space: 1+ ⌈log2 f⌉ bits per record (f = number of fields in record)

35/41

Page 116: Data Types & Memory Management - Principles of Programming ...nzeh/Teaching/3136/Slides/data-types.pdf · MEMORYLAYOUTOFRECORDS Aligned(fixedordering): name atomic_number atomic_weight

SPACE-EFFICIENT MARK AND SWEEP

null

null

curr

back

back = null

null

currback

curr

back

currback

null null

curr

null null

curr

null null

curr

W X

U V

S T

R

Space: 1+ ⌈log2 f⌉ bits per record (f = number of fields in record)

35/41

Page 117: Data Types & Memory Management - Principles of Programming ...nzeh/Teaching/3136/Slides/data-types.pdf · MEMORYLAYOUTOFRECORDS Aligned(fixedordering): name atomic_number atomic_weight

SPACE-EFFICIENT MARK AND SWEEP

null

null

currback

back = null

null

currback curr

back

currback

null null curr

null null

curr

null null

curr

W X

U V

S T

R

Space: 1+ ⌈log2 f⌉ bits per record (f = number of fields in record)

35/41

Page 118: Data Types & Memory Management - Principles of Programming ...nzeh/Teaching/3136/Slides/data-types.pdf · MEMORYLAYOUTOFRECORDS Aligned(fixedordering): name atomic_number atomic_weight

SPACE-EFFICIENT MARK AND SWEEP

null

null

currback

back = null

null

currback curr

back

currback

null null curr

null null

curr

null null

curr

W X

U V

S T

R

Space: 1+ ⌈log2 f⌉ bits per record (f = number of fields in record)

35/41

Page 119: Data Types & Memory Management - Principles of Programming ...nzeh/Teaching/3136/Slides/data-types.pdf · MEMORYLAYOUTOFRECORDS Aligned(fixedordering): name atomic_number atomic_weight

SPACE-EFFICIENT MARK AND SWEEP

null

null

curr

back

back = null

null

currback

curr

back

currback

null null

curr

null null

curr

null null

curr

W X

U V

S T

R

Space: 1+ ⌈log2 f⌉ bits per record (f = number of fields in record)

35/41

Page 120: Data Types & Memory Management - Principles of Programming ...nzeh/Teaching/3136/Slides/data-types.pdf · MEMORYLAYOUTOFRECORDS Aligned(fixedordering): name atomic_number atomic_weight

SPACE-EFFICIENT MARK AND SWEEP

null null

curr

back

back = null

null

currback currback

currback

null null

curr

null null

curr

null null

curr

W X

U V

S T

R

Space: 1+ ⌈log2 f⌉ bits per record (f = number of fields in record)

35/41

Page 121: Data Types & Memory Management - Principles of Programming ...nzeh/Teaching/3136/Slides/data-types.pdf · MEMORYLAYOUTOFRECORDS Aligned(fixedordering): name atomic_number atomic_weight

SPACE-EFFICIENT MARK AND SWEEP

null null

curr

back

back = null

null

currback currback

currback

null null

curr

null null

curr

null null

curr

W X

U V

S T

R

Space: 1+ ⌈log2 f⌉ bits per record (f = number of fields in record)

35/41

Page 122: Data Types & Memory Management - Principles of Programming ...nzeh/Teaching/3136/Slides/data-types.pdf · MEMORYLAYOUTOFRECORDS Aligned(fixedordering): name atomic_number atomic_weight

MARK AND SWEEP VARIANT: STOP AND COPY (1)

Stop and copy algorithm

• Heap is divided into two halves. Allocation happens in the first half.• Once the first half is full, start garbage collection and compaction:

• Find useful objects as in standard mark and sweep but without first markingobjects as useless.

• Copy every useful object to the second half of the heap and replace it with atag pointing to the new location.

• Replace every subsequent pointer to this object with a pointer to the newcopy.

• Swap the roles of the two halves.

36/41

Page 123: Data Types & Memory Management - Principles of Programming ...nzeh/Teaching/3136/Slides/data-types.pdf · MEMORYLAYOUTOFRECORDS Aligned(fixedordering): name atomic_number atomic_weight

MARK AND SWEEP VARIANT: STOP AND COPY (2)

Pros/cons:

+ Time proportional to the number of useful objects, not total number ofobjects.

+ Eliminates external fragmentation.± Only half the heap is available for allocation. Not really an issue if we havevirtual memory.

37/41

Page 124: Data Types & Memory Management - Principles of Programming ...nzeh/Teaching/3136/Slides/data-types.pdf · MEMORYLAYOUTOFRECORDS Aligned(fixedordering): name atomic_number atomic_weight

MARK AND SWEEP VARIANT: GENERATIONAL GARBAGE COLLECTION (1)

Generational garbage collection

• Heap is divided into several (often two) regions.• Allocation happens in the first region.• Garbage collection:

• Apply mark and sweep to the first region.• Promote every object that survives a small number (often one) of rounds ofgarbage collection in a region to the next region in a manner similar to stopand copy.

• Inspect (mark and sweep) subsequent regions only if collection in the regionsinspected so far did not free up enough space.

Idea: Most objects are short-lived. Collection mostly inspects only the first regionand is thus cheaper.

38/41

Page 125: Data Types & Memory Management - Principles of Programming ...nzeh/Teaching/3136/Slides/data-types.pdf · MEMORYLAYOUTOFRECORDS Aligned(fixedordering): name atomic_number atomic_weight

MARK AND SWEEP VARIANT: GENERATIONAL GARBAGE COLLECTION (1)

Generational garbage collection

• Heap is divided into several (often two) regions.• Allocation happens in the first region.• Garbage collection:

• Apply mark and sweep to the first region.• Promote every object that survives a small number (often one) of rounds ofgarbage collection in a region to the next region in a manner similar to stopand copy.

• Inspect (mark and sweep) subsequent regions only if collection in the regionsinspected so far did not free up enough space.

Idea: Most objects are short-lived. Collection mostly inspects only the first regionand is thus cheaper.

38/41

Page 126: Data Types & Memory Management - Principles of Programming ...nzeh/Teaching/3136/Slides/data-types.pdf · MEMORYLAYOUTOFRECORDS Aligned(fixedordering): name atomic_number atomic_weight

MARK AND SWEEP VARIANT: GENERATIONAL GARBAGE COLLECTION (1)

Two main issues:

• How to discover useful objects in a region without searching the otherregions?

• How to update pointers from older regions to younger regions whenpromoting objects?

Techniques:

• Disallow pointers from old regions to young regions by moving objectsaround appropriately.

• Keep list of old-to-new pointers. (Requires instrumentation⇒ runtimeoverhead.)

39/41

Page 127: Data Types & Memory Management - Principles of Programming ...nzeh/Teaching/3136/Slides/data-types.pdf · MEMORYLAYOUTOFRECORDS Aligned(fixedordering): name atomic_number atomic_weight

MARK AND SWEEP VARIANT: GENERATIONAL GARBAGE COLLECTION (1)

Two main issues:

• How to discover useful objects in a region without searching the otherregions?

• How to update pointers from older regions to younger regions whenpromoting objects?

Techniques:

• Disallow pointers from old regions to young regions by moving objectsaround appropriately.

• Keep list of old-to-new pointers. (Requires instrumentation⇒ runtimeoverhead.)

39/41

Page 128: Data Types & Memory Management - Principles of Programming ...nzeh/Teaching/3136/Slides/data-types.pdf · MEMORYLAYOUTOFRECORDS Aligned(fixedordering): name atomic_number atomic_weight

CONSERVATIVE GARBAGE COLLECTION

Normally, garbage collectors need to know which positions in an object arepointers. This requires a type descriptor to be stored with each object (spaceoverhead).

Conservative garbage collectionAssume every word is a pointer and mark every memory block referenced bysuch a “pointer” as useful.

This may fail to reclaim useless objects because some integer or other valuehappens to equal the address of the object when interpreted as a pointer.

Statistically, this is rare.

40/41

Page 129: Data Types & Memory Management - Principles of Programming ...nzeh/Teaching/3136/Slides/data-types.pdf · MEMORYLAYOUTOFRECORDS Aligned(fixedordering): name atomic_number atomic_weight

CONSERVATIVE GARBAGE COLLECTION

Normally, garbage collectors need to know which positions in an object arepointers. This requires a type descriptor to be stored with each object (spaceoverhead).

Conservative garbage collectionAssume every word is a pointer and mark every memory block referenced bysuch a “pointer” as useful.

This may fail to reclaim useless objects because some integer or other valuehappens to equal the address of the object when interpreted as a pointer.

Statistically, this is rare.

40/41

Page 130: Data Types & Memory Management - Principles of Programming ...nzeh/Teaching/3136/Slides/data-types.pdf · MEMORYLAYOUTOFRECORDS Aligned(fixedordering): name atomic_number atomic_weight

CONSERVATIVE GARBAGE COLLECTION

Normally, garbage collectors need to know which positions in an object arepointers. This requires a type descriptor to be stored with each object (spaceoverhead).

Conservative garbage collectionAssume every word is a pointer and mark every memory block referenced bysuch a “pointer” as useful.

This may fail to reclaim useless objects because some integer or other valuehappens to equal the address of the object when interpreted as a pointer.

Statistically, this is rare.

40/41

Page 131: Data Types & Memory Management - Principles of Programming ...nzeh/Teaching/3136/Slides/data-types.pdf · MEMORYLAYOUTOFRECORDS Aligned(fixedordering): name atomic_number atomic_weight

CONSERVATIVE GARBAGE COLLECTION

Normally, garbage collectors need to know which positions in an object arepointers. This requires a type descriptor to be stored with each object (spaceoverhead).

Conservative garbage collectionAssume every word is a pointer and mark every memory block referenced bysuch a “pointer” as useful.

This may fail to reclaim useless objects because some integer or other valuehappens to equal the address of the object when interpreted as a pointer.

Statistically, this is rare.

40/41

Page 132: Data Types & Memory Management - Principles of Programming ...nzeh/Teaching/3136/Slides/data-types.pdf · MEMORYLAYOUTOFRECORDS Aligned(fixedordering): name atomic_number atomic_weight

SUMMARY

• A language’s type system determines the data abstractions we can build.

• Common types include records, arrays, lists (built from records), pointers, …

• Pointers to heap-allocated objects require us to manage these objects.

• Garbage collection relieves us of the need to explicitly manageheap-allocated objects but has a runtime cost and is non-trivial.

41/41