ITEC 320 Lecture 12 Higher level usage of pointers.
-
Upload
geraldine-norton -
Category
Documents
-
view
213 -
download
0
Transcript of ITEC 320 Lecture 12 Higher level usage of pointers.
![Page 1: ITEC 320 Lecture 12 Higher level usage of pointers.](https://reader035.fdocuments.us/reader035/viewer/2022062713/56649ccf5503460f9499b744/html5/thumbnails/1.jpg)
ITEC 320
Lecture 12Higher level usage of pointers
![Page 2: ITEC 320 Lecture 12 Higher level usage of pointers.](https://reader035.fdocuments.us/reader035/viewer/2022062713/56649ccf5503460f9499b744/html5/thumbnails/2.jpg)
Pointers
Review
• Linked list– Pointer usage– Package design
![Page 3: ITEC 320 Lecture 12 Higher level usage of pointers.](https://reader035.fdocuments.us/reader035/viewer/2022062713/56649ccf5503460f9499b744/html5/thumbnails/3.jpg)
Pointers
Problems
• What are the major problems with pointers?
![Page 4: ITEC 320 Lecture 12 Higher level usage of pointers.](https://reader035.fdocuments.us/reader035/viewer/2022062713/56649ccf5503460f9499b744/html5/thumbnails/4.jpg)
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 12 Higher level usage of pointers.](https://reader035.fdocuments.us/reader035/viewer/2022062713/56649ccf5503460f9499b744/html5/thumbnails/5.jpg)
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 12 Higher level usage of pointers.](https://reader035.fdocuments.us/reader035/viewer/2022062713/56649ccf5503460f9499b744/html5/thumbnails/6.jpg)
Pointers
Factories
• In your words, what are they?
![Page 7: ITEC 320 Lecture 12 Higher level usage of pointers.](https://reader035.fdocuments.us/reader035/viewer/2022062713/56649ccf5503460f9499b744/html5/thumbnails/7.jpg)
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 12 Higher level usage of pointers.](https://reader035.fdocuments.us/reader035/viewer/2022062713/56649ccf5503460f9499b744/html5/thumbnails/8.jpg)
Pointers
Advantages/Disadvantages
• Why use factories?• What do they mean for your code?• Benefits• Downsides
![Page 9: ITEC 320 Lecture 12 Higher level usage of pointers.](https://reader035.fdocuments.us/reader035/viewer/2022062713/56649ccf5503460f9499b744/html5/thumbnails/9.jpg)
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 12 Higher level usage of pointers.](https://reader035.fdocuments.us/reader035/viewer/2022062713/56649ccf5503460f9499b744/html5/thumbnails/10.jpg)
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 12 Higher level usage of pointers.](https://reader035.fdocuments.us/reader035/viewer/2022062713/56649ccf5503460f9499b744/html5/thumbnails/11.jpg)
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 12 Higher level usage of pointers.](https://reader035.fdocuments.us/reader035/viewer/2022062713/56649ccf5503460f9499b744/html5/thumbnails/12.jpg)
Pointers
What it enables
![Page 13: ITEC 320 Lecture 12 Higher level usage of pointers.](https://reader035.fdocuments.us/reader035/viewer/2022062713/56649ccf5503460f9499b744/html5/thumbnails/13.jpg)
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 12 Higher level usage of pointers.](https://reader035.fdocuments.us/reader035/viewer/2022062713/56649ccf5503460f9499b744/html5/thumbnails/14.jpg)
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 12 Higher level usage of pointers.](https://reader035.fdocuments.us/reader035/viewer/2022062713/56649ccf5503460f9499b744/html5/thumbnails/15.jpg)
Pointers
Cell phones
• How do they work (communication side)
![Page 16: ITEC 320 Lecture 12 Higher level usage of pointers.](https://reader035.fdocuments.us/reader035/viewer/2022062713/56649ccf5503460f9499b744/html5/thumbnails/16.jpg)
Pointers
Cell Networks
![Page 17: ITEC 320 Lecture 12 Higher level usage of pointers.](https://reader035.fdocuments.us/reader035/viewer/2022062713/56649ccf5503460f9499b744/html5/thumbnails/17.jpg)
Pointers
Other scenarios
• Computer networks• Google maps• Facebook friends• Gaming
![Page 18: ITEC 320 Lecture 12 Higher level usage of pointers.](https://reader035.fdocuments.us/reader035/viewer/2022062713/56649ccf5503460f9499b744/html5/thumbnails/18.jpg)
Pointers
Rationale
• How do you model a cell phone network on a computer?
• Why would you want to simulate a cell phone network?
![Page 19: ITEC 320 Lecture 12 Higher level usage of pointers.](https://reader035.fdocuments.us/reader035/viewer/2022062713/56649ccf5503460f9499b744/html5/thumbnails/19.jpg)
Pointers
Graphs
• Composed of vertices and edges• Vertices– Represent an object in a graph
• Edges– A connection between two vertices
![Page 20: ITEC 320 Lecture 12 Higher level usage of pointers.](https://reader035.fdocuments.us/reader035/viewer/2022062713/56649ccf5503460f9499b744/html5/thumbnails/20.jpg)
Pointers
Variations
• Weighted graph– Toll road– Hotel cost– Identifiers
• Possible usage scenarios?
![Page 21: ITEC 320 Lecture 12 Higher level usage of pointers.](https://reader035.fdocuments.us/reader035/viewer/2022062713/56649ccf5503460f9499b744/html5/thumbnails/21.jpg)
Pointers
Methods of implementation
• Arrays• Pointers• Benefits of each approach• Downsides of each approach
![Page 22: ITEC 320 Lecture 12 Higher level usage of pointers.](https://reader035.fdocuments.us/reader035/viewer/2022062713/56649ccf5503460f9499b744/html5/thumbnails/22.jpg)
Pointers
Code
• Should we use a package?• What about generics?
![Page 23: ITEC 320 Lecture 12 Higher level usage of pointers.](https://reader035.fdocuments.us/reader035/viewer/2022062713/56649ccf5503460f9499b744/html5/thumbnails/23.jpg)
Pointers
Searching
• How do you find information in a graph?
Destination
Start
![Page 24: ITEC 320 Lecture 12 Higher level usage of pointers.](https://reader035.fdocuments.us/reader035/viewer/2022062713/56649ccf5503460f9499b744/html5/thumbnails/24.jpg)
Pointers
Breadth first
• For each node I am connected to– Is this the node I’m looking for?
• If I didn’t find it– For each node I am connected to• Call breadth first search on it
![Page 25: ITEC 320 Lecture 12 Higher level usage of pointers.](https://reader035.fdocuments.us/reader035/viewer/2022062713/56649ccf5503460f9499b744/html5/thumbnails/25.jpg)
Pointers
Depth first
• If I am the node searched for, stop and return
• For each node I am connected to– Call depth first search on that node
![Page 26: ITEC 320 Lecture 12 Higher level usage of pointers.](https://reader035.fdocuments.us/reader035/viewer/2022062713/56649ccf5503460f9499b744/html5/thumbnails/26.jpg)
Pointers
Issues
• What are some of the issues that might happen with searching?
• How do you implement each way?– Stacks / Recursion / Packages / ?
![Page 27: ITEC 320 Lecture 12 Higher level usage of pointers.](https://reader035.fdocuments.us/reader035/viewer/2022062713/56649ccf5503460f9499b744/html5/thumbnails/27.jpg)
Pointers
More
• How do you pick the best path?– Lowest cost– Highest cost– Cover all points with least overlap
![Page 28: ITEC 320 Lecture 12 Higher level usage of pointers.](https://reader035.fdocuments.us/reader035/viewer/2022062713/56649ccf5503460f9499b744/html5/thumbnails/28.jpg)
Pointers
Summary
• Rationale for graph theory• Approach• Finding algorithms
![Page 29: ITEC 320 Lecture 12 Higher level usage of pointers.](https://reader035.fdocuments.us/reader035/viewer/2022062713/56649ccf5503460f9499b744/html5/thumbnails/29.jpg)
Pointers
Summary
• Memory usage at a higher level