E N D
CIS 330: _ _ _ _ ______ _ _____ / / / /___ (_) __ ____ _____ ____/ / / ____/ _/_/ ____/__ __ / / / / __ \/ / |/_/ / __ `/ __ \/ __ / / / _/_// / __/ /___/ /_ / /_/ / / / / /> < / /_/ / / / / /_/ / / /____/_/ / /__/_ __/_ __/ \____/_/ /_/_/_/|_| \__,_/_/ /_/\__,_/ \____/_/ \____//_/ /_/ Lecture 5: Function Pointers, Subtyping, More Unix Hank Childs, University of Oregon April 16th, 2014
Outline • Announcements/Review • Project 2B • Function Pointers • Subtyping • Project 2C • Finish “Unix Boot Camp” • Bonus Topics
Outline • Announcements/Review • Project 2B • Function Pointers • Subtyping • Project 2C • Finish “Unix Boot Camp” • Bonus Topics
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
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.
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
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%)
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
Outline • Announcements/Review • Project 2B • Function Pointers • Subtyping • Project 2C • Finish “Unix Boot Camp” • Bonus Topics
enum example C keyword “enum” – means enum definition is coming This enum contains 6 different student types semi-colon!!!
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
Unions Why are unions useful?
Questions? • Something unclear from last time? • Questions about HW? • vi tips?
Outline • Announcements/Review • Project 2B • Function Pointers • Subtyping • Project 2C • Finish “Unix Boot Camp” • Bonus Topics
Outline • Announcements/Review • Project 2B • Function Pointers • Subtyping • Project 2C • Finish “Unix Boot Camp” • Bonus Topics
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
Function Pointers vs Conditionals What are the pros and cons of each approach?
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.
Simple-to-Exotic Function Pointer Declarations void (*foo)(void); void (*foo)(int **, char ***); char ** (*foo)(int **, void (*)(int)); These sometimes come up on interviews.
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.
Callback example (Note to Hank: do a Makefile for this program later)
Function Pointers • We are going to use function pointers to accomplish “sub-typing” in Project 2C.
Outline • Announcements/Review • Project 2B • Function Pointers • Subtyping • Project 2C • Finish “Unix Boot Camp” • Bonus Topics
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”
Subtyping: example • Supertype: Shape • Subtypes: • Circle • Rectangle • Triangle
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
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
Outline • Announcements/Review • Project 2B • Function Pointers • Subtyping • Project 2C • Finish “Unix Boot Camp” • Bonus Topics
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.
Outline • Announcements/Review • Project 2B • Function Pointers • Subtyping • Project 2C • Finish “Unix Boot Camp” • Bonus Topics
“.” and “..” • Unix convention: • “.” : the current directory • “..” : the parent directory Quiz: you in /path/to/dirand issue “cd ./.././..”. Where do you end up? Answer: “/path”
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
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
which which: tells you the directory the shell is finding a command in.
Invoking programs in current directory shell works with ./prog_name since it views this as a path. Hence $PATH is ignored.
Trojan Horse Attack • export PATH=.:$PATH • why is this a terrible idea?
Wild Cards • ‘*’ (asterisk) serves as a wild card that does pattern matching
Wild Cards • You can use multiple asterisks for complex patterns
if / then / else / fi • Advanced constructs:
-f and -d • -f : does a file exist? • -d : does a directory exist? example: if [[ ! -d include ]] ; then mkdir include ; fi
Unresolved symbols This means the compiler can’t find the functions cos330, sin330, etc.
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
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…