1 / 64

Hank Childs, University of Oregon

joella
Download Presentation

Hank Childs, University of Oregon

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CIS 330: _ _ _ _ ______ _ _____ / / / /___ (_) __ ____ _____ ____/ / / ____/ _/_/ ____/__ __ / / / / __ \/ / |/_/ / __ `/ __ \/ __ / / / _/_// / __/ /___/ /_ / /_/ / / / / /> < / /_/ / / / / /_/ / / /____/_/ / /__/_ __/_ __/ \____/_/ /_/_/_/|_| \__,_/_/ /_/\__,_/ \____/_/ \____//_/ /_/ Lecture 5: Function Pointers, Subtyping, More Unix Hank Childs, University of Oregon April 16th, 2014

  2. Outline • Announcements/Review • Project 2B • Function Pointers • Subtyping • Project 2C • Finish “Unix Boot Camp” • Bonus Topics

  3. Outline • Announcements/Review • Project 2B • Function Pointers • Subtyping • Project 2C • Finish “Unix Boot Camp” • Bonus Topics

  4. Announcement: HW • I am slowing down Proj 2A, 2B timelines • Proj 2A: now due today, not Monday • Proj 2B: now due Sat 4/19, not Fri 4/18. • (please start on it sooner) • Proj 2C: due Tues 4/22

  5. Announcement: Grading • I have been slow to get to grading • I know getting feedback via grading is important • I am not looking to punish anyone with grading • Our collective goal: excellence in Unix, C, and C++, enabling success in 400 classes and in the workplace. • … we are working together, not against each other • If you are working hard, then I am happy • I will not be finding ways to deduct points.

  6. Announcements • Matt OH: Mon/Tues 4-5:30, DES 232 • Pavel OH: Weds/Thurs 4-5, DES 100 • Hank OH: Thurs 11-12, Fri 10-11, 12:30-1:30 Please come to OH

  7. Luks Programming Contest • 18th Annual Luks Programming Contest • April 19th • Deschutes Room 100 • Benefits • Fun • Food • Experience • T-shirt • Extra credit for CIS 330 (2%)

  8. Volunteers Needed • Project 3 is coming soon • Image processing pipeline • We will need to read/write PNG image files • This will involve installing libraries • Need student presentations on package managers • Ideally one 5 minute presentation/show-n-tell for multiple package managers Each presenter will earn 2% extra credit

  9. Outline • Announcements/Review • Project 2B • Function Pointers • Subtyping • Project 2C • Finish “Unix Boot Camp” • Bonus Topics

  10. Example

  11. enum example C keyword “enum” – means enum definition is coming This enum contains 6 different student types semi-colon!!!

  12. struct syntax “.” accesses data members for a struct C keyword “struct” – means struct definition is coming This struct contains 6 doubles, meaning it is 48 bytes semi-colon!!! Declaring an instance

  13. Unions Why are unions useful?

  14. Unions Example

  15. Questions? • Something unclear from last time? • Questions about HW? • vi tips?

  16. Outline • Announcements/Review • Project 2B • Function Pointers • Subtyping • Project 2C • Finish “Unix Boot Camp” • Bonus Topics

  17. Project 2B

  18. Outline • Announcements/Review • Project 2B • Function Pointers • Subtyping • Project 2C • Finish “Unix Boot Camp” • Bonus Topics

  19. Function Pointers • Idea: • You have a pointer to a function • This pointer can change based on circumstance • When you call the function pointer, it is liking calling a known function

  20. Function Pointer Example

  21. Function Pointers vs Conditionals What are the pros and cons of each approach?

  22. Function Pointer Example #2 Function pointer Part of function signature Don’t be scared of extra ‘*’s … they just come about because of pointers in the arguments or return values.

  23. Simple-to-Exotic Function Pointer Declarations void (*foo)(void); void (*foo)(int **, char ***); char ** (*foo)(int **, void (*)(int)); These sometimes come up on interviews.

  24. Callbacks • Callbacks: function that is called when a condition is met • Commonly used when interfacing between modules that were developed separately. • … libraries use callbacks and developers who use the libraries “register” callbacks.

  25. Callback example

  26. Callback example (Note to Hank: do a Makefile for this program later)

  27. Function Pointers • We are going to use function pointers to accomplish “sub-typing” in Project 2C.

  28. Outline • Announcements/Review • Project 2B • Function Pointers • Subtyping • Project 2C • Finish “Unix Boot Camp” • Bonus Topics

  29. Subtyping • Type: a data type (int, float, structs) • Subtype / supertype: • Supertype: the abstraction of a type • (not specific) • Subtype: a concrete implementation of the supertype • (specific) The fancy term for this is “subtype polymorphism”

  30. Subtyping: example • Supertype: Shape • Subtypes: • Circle • Rectangle • Triangle

  31. Subtyping works via interfaces • Must define an interface for supertype/subtypes • Interfaces are the functions you can call on the supertype/subtypes • The set of functions is fixed • Every subtype must define all functions

  32. Subtyping • I write my routines to the supertype interface • All subtypes can automatically use this code • Don’t have to modify code when new supertypes are added • Example: • I wrote code about Shapes. • I don’t care about details of subtypes (Triangle, Rectangle, Circle) • When new subtypes are added (Square), my code doesn’t change

  33. Outline • Announcements/Review • Project 2B • Function Pointers • Subtyping • Project 2C • Finish “Unix Boot Camp” • Bonus Topics

  34. Project 2C • You will extend Project 2B • You will do Subtyping • You will make a union of all the structs • You will make a struct of function pointers • This will enable subtyping • Goal: driver program works on “Shape”s and doesn’t need to know if it is a Circle, Triangle, or Rectangle.

  35. Outline • Announcements/Review • Project 2B • Function Pointers • Subtyping • Project 2C • Finish “Unix Boot Camp” • Bonus Topics

  36. “.” and “..” • Unix convention: • “.” : the current directory • “..” : the parent directory Quiz: you in /path/to/dirand issue “cd ./.././..”. Where do you end up? Answer: “/path”

  37. pwd and $PWD • pwd: unix command that returns the “present working directory” • $PWD : environment variable that contains the present working directory • $OLDPWD : environment variable that contains the previous present working directory • “-” : shortcut for the previous PWD

  38. PATH environment variable “tr”: Unix command for replacing characters (translating characters). When the shell wants to invoke a command, it searches for the command in the path

  39. which which: tells you the directory the shell is finding a command in.

  40. Invoking programs in current directory shell works with ./prog_name since it views this as a path. Hence $PATH is ignored.

  41. Invoking programs in current directory

  42. Trojan Horse Attack • export PATH=.:$PATH • why is this a terrible idea?

  43. Wild Cards • ‘*’ (asterisk) serves as a wild card that does pattern matching

  44. Wild Cards • You can use multiple asterisks for complex patterns

  45. if / then / else / fi • Advanced constructs:

  46. for / do / done

  47. -f and -d • -f : does a file exist? • -d : does a directory exist? example: if [[ ! -d include ]] ; then mkdir include ; fi

  48. Unresolved symbols This means the compiler can’t find the functions cos330, sin330, etc.

  49. How linkers work (for static libraries) gcc –o output file1.o –llibmath330 –lm • Look at file1.o • Make a list of unresolved symbols • Look at libmath330.a • Retain any symbols that are in current “unresolved list” • Add new unresolved that are needed from retaining libmath330.a’s symbols • Repeat for libm.a • If any unresolved symbols, then issue error

  50. How linkers work (for static libraries) gcc –o output –llibmath330 –lm file1.o • Look at libmath330. • No unresolved symbols at this point, so nothing to do • All of libmath330 essentially thrown out • Ditto for libm.a • When studying file1.o, we finally find main. • But we have unresolved symbols, so error…

More Related