15
ITEC 320 Lecture 19 Higher level usage of pointers

ITEC 320 Lecture 19 Higher level usage of pointers

Embed Size (px)

Citation preview

Page 1: ITEC 320 Lecture 19 Higher level usage of pointers

ITEC 320

Lecture 19Higher level usage of pointers

Page 2: ITEC 320 Lecture 19 Higher level usage of pointers

Pointers

Review

• Linked list– Pointer usage– Package design

• HW 1–Write a doubly linked list that has insert

before / after and delete methods– Code will be posted by tomorrow– Due next Monday at 10PM

Page 3: ITEC 320 Lecture 19 Higher level usage of pointers

Pointers

Problems

• What are the major problems with pointers?

Page 4: ITEC 320 Lecture 19 Higher level usage of pointers

Pointers

Reference counting

• Each object has to be uniquely tracked in a system

• The number of pointers to that object is also counted

• Whenever a pointer is disposed, the reference count is decreased

• What happens when it reaches 0?• What problem does this resolve?

Page 5: ITEC 320 Lecture 19 Higher level usage of pointers

Pointers

Smart pointers

• Contain regular pointers• Also contain number of other

pointers that are pointing to the object

• Or do they…–What if an object contains the number of

pointers to itself?– Benefits / downsides

• Designs for this

Page 6: ITEC 320 Lecture 19 Higher level usage of pointers

Pointers

Factories

• In your words, what are they?

Page 7: ITEC 320 Lecture 19 Higher level usage of pointers

Pointers

Approaches

• Function that returns an object• Function that takes an object and

returns a pointer• Function that takes an id and returns

a pointer to it• Hash map for memory addresses and

# of accesses• Others?

Page 8: ITEC 320 Lecture 19 Higher level usage of pointers

Pointers

Advantages/Disadvantages

• Why use factories?• What do they mean for your code?• Benefits• Downsides

Page 9: ITEC 320 Lecture 19 Higher level usage of pointers

Pointers

Flyweight

• What does the word bring to mind?• One object / pointer reused lots and

lots of times• Similar to NULL except it is a type /

pointer• What reasons do you think you would

use a flyweight object?• Benefits / Downsides

Page 10: ITEC 320 Lecture 19 Higher level usage of pointers

Pointers

Disk buffer

• How many GB does a DVD hold?• 3 DVD game• How does that game load everything

into RAM? Or does it load everything into RAM?

• Memory Mapping to HD idea

Page 11: ITEC 320 Lecture 19 Higher level usage of pointers

Pointers

How it works

• Several blocks of data reserved in memory

• Each block maps to a unique block on a HD

• Data is requested, it is loaded from HD – If it is in list, use it, move to MRU

position– If it isn’t, load into least recently used

block and move it to the MRU position– Linked list of blocks?

Page 12: ITEC 320 Lecture 19 Higher level usage of pointers

Pointers

What it enables

Page 13: ITEC 320 Lecture 19 Higher level usage of pointers

Pointers

Design patterns

• Not language specific features• Encompasses a particular idea• May or may not be heavily supported

in a language– Java supports smart pointers by default– C++ requires a separate class

• Not a silver bullet

Page 14: ITEC 320 Lecture 19 Higher level usage of pointers

Pointers

Command pattern

• Signals when an action needs to be performed

CommandCentral

Contains pointers to objectsContains list of commandsTold to execute X, it actuallydoes it

Page 15: ITEC 320 Lecture 19 Higher level usage of pointers

Pointers

Summary

• Memory usage at a higher level