lecture 12: pointers to functions...

57
Hank Childs, University of Oregon November 7, 2019 Lecture 12: Pointers to Functions Complexity __. __ .__ __. __ ___ ___ ______ ___ .__ __. _______ _______ ___ .___________. ___ | | | | | \| | | | \ \/ / / | / \ | \| | | \ | \ / \ | | / \ | | | | | \| | | | \ V / | ,----' /^\ | \| | | .--. | | .--. | / ^ \ `---| |----` / ^ \ | | | | | . ` | | | > < || / /_\ \ | . ` | | | | | | | | | / /_\ \ || / /_\ \ | `--' | | |\ | | |/.\ __ | `----.__ / _____ \ | |\ | | '--' | | '--' | / _____ \ || / _____ \ \______/ |__| \__| |__| /__/ \__\ (_ ) \______(_ ) /__/ \__\ |__| \__| |_______/ |_______/ /__/ \__\ |__| /__/ \__\ _______.___________..______ __ __ ______ .___________. __ __ .______ _______ _______. / | || _ \ ||||/ || || | | | | _ \ | ____| / | | (----`---| |----`| |_) | | | | | | ,----'`---| |----`| | | | | |_) | | |__ | (----` \ \ || | / | | | | | | || | | | | | / | __| \ \ .----) | || | |\ \----.| `--' | | `----. || | `--' | | |\ \----.| |____.----) | |_______/ |__| | _| `._____| \______/ \______| |__| \______/ | _| `._____||_______|_______/

Upload: others

Post on 08-May-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lecture 12: Pointers to Functions Complexityix.cs.uoregon.edu/~Hank/212/Lectures/CIS212_F19_Lec12.pdfBig O Notation (1/3): Important Computer Science Concept •If input data size

Hank Childs, University of OregonNovember 7, 2019

Lecture 12:Pointers to Functions

Complexity

__. __ .__ __. __ ___ ___ ______ ___ .__ __. _______ _______ ___ .___________. ___| | | | | \ | | | | \ \ / / / | / \ | \ | | | \ | \ / \ | | / \| | | | | \| | | | \ V / | ,----' / ^ \ | \| | | .--. | | .--. | / ^ \ `---| |----` / ^ \| | | | | . ` | | | > < | | / /_\ \ | . ` | | | | | | | | | / /_\ \ | | / /_\ \| `--' | | |\ | | | / . \ __ | `----.__ / _____ \ | |\ | | '--' | | '--' | / _____ \ | | / _____ \\______/ |__| \__| |__| /__/ \__\ (_ ) \______(_ ) /__/ \__\ |__| \__| |_______/ |_______/ /__/ \__\ |__| /__/ \__\

_______.___________..______ __ __ ______ .___________. __ __ .______ _______ _______. / | || _ \ | | | | / || || | | | | _ \ | ____| / | | (----`---| |----`| |_) | | | | | | ,----'`---| |----`| | | | | |_) | | |__ | (----` \ \ | | | / | | | | | | | | | | | | | / | __| \ \

.----) | | | | |\ \----.| `--' | | `----. | | | `--' | | |\ \----.| |____.----) ||_______/ |__| | _| `._____| \______/ \______| |__| \______/ | _| `._____||_______|_______/

Page 2: Lecture 12: Pointers to Functions Complexityix.cs.uoregon.edu/~Hank/212/Lectures/CIS212_F19_Lec12.pdfBig O Notation (1/3): Important Computer Science Concept •If input data size

TalkaboutQ1frommidterm

Page 3: Lecture 12: Pointers to Functions Complexityix.cs.uoregon.edu/~Hank/212/Lectures/CIS212_F19_Lec12.pdfBig O Notation (1/3): Important Computer Science Concept •If input data size

Project2G

Page 4: Lecture 12: Pointers to Functions Complexityix.cs.uoregon.edu/~Hank/212/Lectures/CIS212_F19_Lec12.pdfBig O Notation (1/3): Important Computer Science Concept •If input data size

QuizonTuesday,Nov12

• TherewillbeaquizTuesdayNov12.• Itwillbeworth3points.• ItwillbeonfileI/O.• Nonotes.• Expectingyoutoknowfopen/fread/fwrite/etc.

Page 5: Lecture 12: Pointers to Functions Complexityix.cs.uoregon.edu/~Hank/212/Lectures/CIS212_F19_Lec12.pdfBig O Notation (1/3): Important Computer Science Concept •If input data size

Reading:Chapters5and6

• Iwillbefollowingthetextbookmorecloselythenextfewweeks

Page 6: Lecture 12: Pointers to Functions Complexityix.cs.uoregon.edu/~Hank/212/Lectures/CIS212_F19_Lec12.pdfBig O Notation (1/3): Important Computer Science Concept •If input data size

Callbyvalue/callbyreference

• Referstohowparametersarepassedtoafunction.– Callbyvalue:sendthevalueofthevariableasafunctionparameter• Sideeffectsinthatfunctiondon’t affectthevariableinthecallingfunction

– Callbyreference:sendareference(pointer)asafunctionparameter• Sideeffectsinthatfunctionaffectthevariableinthecallingfunction

Page 7: Lecture 12: Pointers to Functions Complexityix.cs.uoregon.edu/~Hank/212/Lectures/CIS212_F19_Lec12.pdfBig O Notation (1/3): Important Computer Science Concept •If input data size

CallbyValue

Cdoesnotcarethatfoohasan

argumentvariablecalled“x”andmainhasan

automaticvariablecalled“x”.TheyhaveNOrelation.

Page 8: Lecture 12: Pointers to Functions Complexityix.cs.uoregon.edu/~Hank/212/Lectures/CIS212_F19_Lec12.pdfBig O Notation (1/3): Important Computer Science Concept •If input data size

Callbyreference

Page 9: Lecture 12: Pointers to Functions Complexityix.cs.uoregon.edu/~Hank/212/Lectures/CIS212_F19_Lec12.pdfBig O Notation (1/3): Important Computer Science Concept •If input data size

Preprocessor

• Preprocessor:– takesaninputprogram– producesanotherprogram(whichisthencompiled)

• Chasaseparatelanguageforpreprocessing– DifferentsyntaxthanC– Usesmacros(“#”)

macro(“macroinstruction”):ruleforreplacinginputcharacterswithoutputcharacters

Page 10: Lecture 12: Pointers to Functions Complexityix.cs.uoregon.edu/~Hank/212/Lectures/CIS212_F19_Lec12.pdfBig O Notation (1/3): Important Computer Science Concept •If input data size

#include

• compilercanonlycompileonefileatafile• takesanotherfileandincludesitinthecurrentfile

• thefileisa“header”file– itcontainsfunctionprototypes– afunctionprototypedeclaresafunctionexists,butnothowitisimplemented

Page 11: Lecture 12: Pointers to Functions Complexityix.cs.uoregon.edu/~Hank/212/Lectures/CIS212_F19_Lec12.pdfBig O Notation (1/3): Important Computer Science Concept •If input data size

Demonstrate#include

#include<stdio.h>int main(){printf("Helloworld\n");

}

%gcc –Eprintf.c

Page 12: Lecture 12: Pointers to Functions Complexityix.cs.uoregon.edu/~Hank/212/Lectures/CIS212_F19_Lec12.pdfBig O Notation (1/3): Important Computer Science Concept •If input data size

FunctionPrototype

Page 13: Lecture 12: Pointers to Functions Complexityix.cs.uoregon.edu/~Hank/212/Lectures/CIS212_F19_Lec12.pdfBig O Notation (1/3): Important Computer Science Concept •If input data size

PreprocessorPhases

• Resolve#includes• Conditionalcompilation(#ifdef)• Macroreplacement• Specialmacros

Page 14: Lecture 12: Pointers to Functions Complexityix.cs.uoregon.edu/~Hank/212/Lectures/CIS212_F19_Lec12.pdfBig O Notation (1/3): Important Computer Science Concept •If input data size

#definecompilation

Thisisanexampleofmacroreplacement.

Page 15: Lecture 12: Pointers to Functions Complexityix.cs.uoregon.edu/~Hank/212/Lectures/CIS212_F19_Lec12.pdfBig O Notation (1/3): Important Computer Science Concept •If input data size

#defineviagcc command-lineoption

Page 16: Lecture 12: Pointers to Functions Complexityix.cs.uoregon.edu/~Hank/212/Lectures/CIS212_F19_Lec12.pdfBig O Notation (1/3): Important Computer Science Concept •If input data size

Conflicting–Dand#define

Page 17: Lecture 12: Pointers to Functions Complexityix.cs.uoregon.edu/~Hank/212/Lectures/CIS212_F19_Lec12.pdfBig O Notation (1/3): Important Computer Science Concept •If input data size

Conditionalcompilation

Page 18: Lecture 12: Pointers to Functions Complexityix.cs.uoregon.edu/~Hank/212/Lectures/CIS212_F19_Lec12.pdfBig O Notation (1/3): Important Computer Science Concept •If input data size

Conditionalcompilationcontrolledviacompilerflags

Page 19: Lecture 12: Pointers to Functions Complexityix.cs.uoregon.edu/~Hank/212/Lectures/CIS212_F19_Lec12.pdfBig O Notation (1/3): Important Computer Science Concept •If input data size

FunctionPointers

• Idea:– Youhaveapointertoafunction– Thispointercanchangebasedoncircumstance–Whenyoucallthefunctionpointer,itislikecallingaknownfunction

Page 20: Lecture 12: Pointers to Functions Complexityix.cs.uoregon.edu/~Hank/212/Lectures/CIS212_F19_Lec12.pdfBig O Notation (1/3): Important Computer Science Concept •If input data size

FunctionPointerExample

Page 21: Lecture 12: Pointers to Functions Complexityix.cs.uoregon.edu/~Hank/212/Lectures/CIS212_F19_Lec12.pdfBig O Notation (1/3): Important Computer Science Concept •If input data size

FunctionPointerExample#2

Functionpointer Partoffunctionsignature

Don’tbescaredofextra‘*’s…theyjustcomeaboutbecauseofpointersintheargumentsorreturnvalues.

Page 22: Lecture 12: Pointers to Functions Complexityix.cs.uoregon.edu/~Hank/212/Lectures/CIS212_F19_Lec12.pdfBig O Notation (1/3): Important Computer Science Concept •If input data size

Simple-to-ExoticFunctionPointerDeclarations

void(*foo)(void);void(*foo)(int **,char***);char**(*foo)(int **,void(*)(int));

Thesesometimescomeuponinterviews.

Page 23: Lecture 12: Pointers to Functions Complexityix.cs.uoregon.edu/~Hank/212/Lectures/CIS212_F19_Lec12.pdfBig O Notation (1/3): Important Computer Science Concept •If input data size

FunctionPointersvs Conditionals

Whataretheprosandconsofeachapproach?

Page 24: Lecture 12: Pointers to Functions Complexityix.cs.uoregon.edu/~Hank/212/Lectures/CIS212_F19_Lec12.pdfBig O Notation (1/3): Important Computer Science Concept •If input data size

Callbacks

• Callbacks:functionthatiscalledwhenaconditionismet– Commonlyusedwheninterfacingbetweenmodulesthatweredevelopedseparately.

– …librariesusecallbacksanddeveloperswhousethelibraries“register”callbacks.

Page 25: Lecture 12: Pointers to Functions Complexityix.cs.uoregon.edu/~Hank/212/Lectures/CIS212_F19_Lec12.pdfBig O Notation (1/3): Important Computer Science Concept •If input data size

Callbackexample

Page 26: Lecture 12: Pointers to Functions Complexityix.cs.uoregon.edu/~Hank/212/Lectures/CIS212_F19_Lec12.pdfBig O Notation (1/3): Important Computer Science Concept •If input data size

Callbackexample

Page 27: Lecture 12: Pointers to Functions Complexityix.cs.uoregon.edu/~Hank/212/Lectures/CIS212_F19_Lec12.pdfBig O Notation (1/3): Important Computer Science Concept •If input data size

WhatisaDataStructure?

Page 28: Lecture 12: Pointers to Functions Complexityix.cs.uoregon.edu/~Hank/212/Lectures/CIS212_F19_Lec12.pdfBig O Notation (1/3): Important Computer Science Concept •If input data size

WhatisaDataStructure?

• Datastructuredefinitions– Textbook:“asystematicwaytoorganizedata”–Wikipedia:“dataorganization,managementandstorageformatthatenablesefficientaccessandmodification”

Page 29: Lecture 12: Pointers to Functions Complexityix.cs.uoregon.edu/~Hank/212/Lectures/CIS212_F19_Lec12.pdfBig O Notation (1/3): Important Computer Science Concept •If input data size

WeAreAlreadyFamiliarWithSomeDataStructures

• Arrays• FromPython– List– Tuple– Dictionary– Set

Page 30: Lecture 12: Pointers to Functions Complexityix.cs.uoregon.edu/~Hank/212/Lectures/CIS212_F19_Lec12.pdfBig O Notation (1/3): Important Computer Science Concept •If input data size

KeyConcept

• Itorganizesdata• Itenablesefficientaccess–Whatdoesefficientmean??

Page 31: Lecture 12: Pointers to Functions Complexityix.cs.uoregon.edu/~Hank/212/Lectures/CIS212_F19_Lec12.pdfBig O Notation (1/3): Important Computer Science Concept •If input data size

Example:DataStructure:ArraysOperation:Search

Page 32: Lecture 12: Pointers to Functions Complexityix.cs.uoregon.edu/~Hank/212/Lectures/CIS212_F19_Lec12.pdfBig O Notation (1/3): Important Computer Science Concept •If input data size

IsItEfficient?:TwoSub-questions

• 1)Howlongdoesthistaketorun?– (howdowemeasurethis?)

• 2)Couldwedoitwithlessoperations?

Page 33: Lecture 12: Pointers to Functions Complexityix.cs.uoregon.edu/~Hank/212/Lectures/CIS212_F19_Lec12.pdfBig O Notation (1/3): Important Computer Science Concept •If input data size

IsItEfficient?:TwoSub-questions

• 1)Howlongdoesthistaketorun?– (howdowemeasurethis?)

• 2)Couldwedoitwithlessoperations?

Page 34: Lecture 12: Pointers to Functions Complexityix.cs.uoregon.edu/~Hank/212/Lectures/CIS212_F19_Lec12.pdfBig O Notation (1/3): Important Computer Science Concept •If input data size

HowLongDoesThisTakeToRun?

• Oneanswer:timeit!

Page 35: Lecture 12: Pointers to Functions Complexityix.cs.uoregon.edu/~Hank/212/Lectures/CIS212_F19_Lec12.pdfBig O Notation (1/3): Important Computer Science Concept •If input data size

Unixcommand:time

Courtesy:Sventek textbook

Page 36: Lecture 12: Pointers to Functions Complexityix.cs.uoregon.edu/~Hank/212/Lectures/CIS212_F19_Lec12.pdfBig O Notation (1/3): Important Computer Science Concept •If input data size

Anotheroption:addtimingstoyourprogram!

• (note:nowtakingafewslideaside)

Page 37: Lecture 12: Pointers to Functions Complexityix.cs.uoregon.edu/~Hank/212/Lectures/CIS212_F19_Lec12.pdfBig O Notation (1/3): Important Computer Science Concept •If input data size

gettimeofday

(therearelotsofUnixsystemcalls,whichdolotsofdifferentthings)

Page 38: Lecture 12: Pointers to Functions Complexityix.cs.uoregon.edu/~Hank/212/Lectures/CIS212_F19_Lec12.pdfBig O Notation (1/3): Important Computer Science Concept •If input data size

gettimeofday example

Page 39: Lecture 12: Pointers to Functions Complexityix.cs.uoregon.edu/~Hank/212/Lectures/CIS212_F19_Lec12.pdfBig O Notation (1/3): Important Computer Science Concept •If input data size

gettimeofday example

Page 40: Lecture 12: Pointers to Functions Complexityix.cs.uoregon.edu/~Hank/212/Lectures/CIS212_F19_Lec12.pdfBig O Notation (1/3): Important Computer Science Concept •If input data size

gettimeofday example

Page 41: Lecture 12: Pointers to Functions Complexityix.cs.uoregon.edu/~Hank/212/Lectures/CIS212_F19_Lec12.pdfBig O Notation (1/3): Important Computer Science Concept •If input data size

gettimeofday example

Page 42: Lecture 12: Pointers to Functions Complexityix.cs.uoregon.edu/~Hank/212/Lectures/CIS212_F19_Lec12.pdfBig O Notation (1/3): Important Computer Science Concept •If input data size

HowLongDoesThisTakeToRun?

• Oneanswer:timeit!• Question:whatifIchangethedata?–Whatifwehave8000studentsinsteadof80students?

• Newidea:analyzetheprogram

Page 43: Lecture 12: Pointers to Functions Complexityix.cs.uoregon.edu/~Hank/212/Lectures/CIS212_F19_Lec12.pdfBig O Notation (1/3): Important Computer Science Concept •If input data size

AnalyzetheProgram

• Howmanyoperationsdoesthisperform?• Assumestrcmp takes50operations• AndnumStudents is80• Then~80*50=4000operation

Page 44: Lecture 12: Pointers to Functions Complexityix.cs.uoregon.edu/~Hank/212/Lectures/CIS212_F19_Lec12.pdfBig O Notation (1/3): Important Computer Science Concept •If input data size

BigONotation(1/3):ImportantComputerScienceConcept

• Ifinputdatasizeis“N”,thenyoucandescribehowmanyoperationsoccurusingN

• If“numStudents”isN,andstrcmp takes50operations,then50N

Page 45: Lecture 12: Pointers to Functions Complexityix.cs.uoregon.edu/~Hank/212/Lectures/CIS212_F19_Lec12.pdfBig O Notation (1/3): Important Computer Science Concept •If input data size

BigONotation(2/3):ImportantComputerScienceConcept

• Idea#1behindBigO:don’tworryaboutconstants• Idea#2behindBigO:justsayhowmanyoperationswithrespecttoN(numberofdataelements)

• Answer:O(N)– Thisissosimple,almostnotuseful… neednewexample!

Page 46: Lecture 12: Pointers to Functions Complexityix.cs.uoregon.edu/~Hank/212/Lectures/CIS212_F19_Lec12.pdfBig O Notation (1/3): Important Computer Science Concept •If input data size

BigONotation(3/3):ImportantComputerScienceConcept

• Thisiscalled“AsymptoticAnalysis”–Why?… whathappenswhenNgets“large”?

Page 47: Lecture 12: Pointers to Functions Complexityix.cs.uoregon.edu/~Hank/212/Lectures/CIS212_F19_Lec12.pdfBig O Notation (1/3): Important Computer Science Concept •If input data size

Morecomplexexample

• Howmanyoperationsnow?• numStudents*numStudents*50(about)• à O(n2)

Page 48: Lecture 12: Pointers to Functions Complexityix.cs.uoregon.edu/~Hank/212/Lectures/CIS212_F19_Lec12.pdfBig O Notation (1/3): Important Computer Science Concept •If input data size

IsThisBetter?

• Howmanyoperationsnow?• numStudents*numStudents*25(about)• STILLà O(n2)

Page 49: Lecture 12: Pointers to Functions Complexityix.cs.uoregon.edu/~Hank/212/Lectures/CIS212_F19_Lec12.pdfBig O Notation (1/3): Important Computer Science Concept •If input data size

ComparingOurTwoImplementations

• Answer:secondversionisabouttwiceasfast– ButbothO(n2),somaybenotahugedifference

Page 50: Lecture 12: Pointers to Functions Complexityix.cs.uoregon.edu/~Hank/212/Lectures/CIS212_F19_Lec12.pdfBig O Notation (1/3): Important Computer Science Concept •If input data size

Newdatastructure:sortedarray

• Likeanarray,buteverythingissorted• Unsortedarray– int X[6]={4,1,3,7,9,2};

• Sortedarray– int X[6]={1,2,3,4,7,9};

Page 51: Lecture 12: Pointers to Functions Complexityix.cs.uoregon.edu/~Hank/212/Lectures/CIS212_F19_Lec12.pdfBig O Notation (1/3): Important Computer Science Concept •If input data size

CanWeImproveOnThisOneWithAnUnsortedArray?

• Probablynot,but…

Page 52: Lecture 12: Pointers to Functions Complexityix.cs.uoregon.edu/~Hank/212/Lectures/CIS212_F19_Lec12.pdfBig O Notation (1/3): Important Computer Science Concept •If input data size

ButWeCanImproveWithaSortedArray…

Page 53: Lecture 12: Pointers to Functions Complexityix.cs.uoregon.edu/~Hank/212/Lectures/CIS212_F19_Lec12.pdfBig O Notation (1/3): Important Computer Science Concept •If input data size

Howmanyoperationsdidthistake?

• StartwithNelements• After1iteration,N/2elements• After2iterations,N/4elements• After3iterations,N/8elements• After4iterations,N/16elements• Etc…• Afterlog2Niterations,1element• à O(logn)

Page 54: Lecture 12: Pointers to Functions Complexityix.cs.uoregon.edu/~Hank/212/Lectures/CIS212_F19_Lec12.pdfBig O Notation (1/3): Important Computer Science Concept •If input data size

Buttherewasworktotakeanarrayandmakeasortedarray

• Howlongdoesittaketosort?• Sort:complexityvaries:O(nlogn)toO(n2)– Project2Bexample:O(n2)– Mostrealworld:O(nlogn)

• So:wecoulddoIsStudentInClass onunsortedarrayforO(n)

• Or:sortarray(costO(nlogn))anddoIsStudentInClass forO(logn)

• à weonlywanttodoasortifweplantocallIsStudentInClass abunchoftimes

Page 55: Lecture 12: Pointers to Functions Complexityix.cs.uoregon.edu/~Hank/212/Lectures/CIS212_F19_Lec12.pdfBig O Notation (1/3): Important Computer Science Concept •If input data size

CanwegetabetterBigOforthisone?

• ‘for’loopovernumStudents:O(n)• Combined:O(nlogn)+O(n)à O(nlogn)– (asymptoticanalysis)

Page 56: Lecture 12: Pointers to Functions Complexityix.cs.uoregon.edu/~Hank/212/Lectures/CIS212_F19_Lec12.pdfBig O Notation (1/3): Important Computer Science Concept •If input data size

Courtesy:Sventek textbook

Page 57: Lecture 12: Pointers to Functions Complexityix.cs.uoregon.edu/~Hank/212/Lectures/CIS212_F19_Lec12.pdfBig O Notation (1/3): Important Computer Science Concept •If input data size

IsItEfficient?:TwoSub-questions

• 1)Howlongdoesthistaketorun?– (howdowemeasurethis?)

• 2)Couldwedoitwithlessoperations?

• Unsortedarraysweremoreefficientforsearch,buttherewasworkincreatingthem– Differentdatastructuresareefficientfordifferentoperations

– Hence,wehavealotofthem