56
APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 25 March 19, 2012

Lecture 25 - Dynamic Graphics Projectjdeber/aps105h/201201/lectures/Lecture25/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 25 March 19,

  • Upload
    others

  • View
    0

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Lecture 25 - Dynamic Graphics Projectjdeber/aps105h/201201/lectures/Lecture25/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 25 March 19,

APS105Winter 2012

Jonathan Deberjdeber -at- cs -dot- toronto -dot- edu

Lecture 25March 19, 2012

Page 2: Lecture 25 - Dynamic Graphics Projectjdeber/aps105h/201201/lectures/Lecture25/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 25 March 19,

2

Today

• !"#$$%&'(

• Commenting

• Testing

• Debugging

Page 3: Lecture 25 - Dynamic Graphics Projectjdeber/aps105h/201201/lectures/Lecture25/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 25 March 19,

Fixed Sizes

• Automatically allocated variables can’t change size

• Until C99, array sizes needed to be fixed at compile time

3

!")*+,#-./0

)*+,*01&#*2'34536,7*(0)*+,#-*/0

Pre-

C99C99

Page 4: Lecture 25 - Dynamic Graphics Projectjdeber/aps105h/201201/lectures/Lecture25/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 25 March 19,

Fixed Sizes

• Automatically allocated variables can’t change size

• Until C99, array sizes needed to be fixed at compile time

3

!")*+,#-./0

)*+,*01&#*2'34536,7*(0)*+,#-*/0

Pre-

C99C99

Page 5: Lecture 25 - Dynamic Graphics Projectjdeber/aps105h/201201/lectures/Lecture25/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 25 March 19,

!"#$$%&'(

• Resizes a =#$$%&'(ed chunk of memory

• Can increase or decrease the size

• Returns a pointer to the resized memory

• :,>,?@AA is equivalent to =#$$%&'(

• 1);",>,B is equivalent to 2!""'(

4

8%)5,9!"#$$%&'8%)5,9:6,1);"<+,1);"(0

)*+,9#,>,=#$$%&'.,9,1);"%2')*+((0

for now think of it as )*+

#,>,!"#$$%&'#6,C,9,1);"%2')*+((0

Page 6: Lecture 25 - Dynamic Graphics Projectjdeber/aps105h/201201/lectures/Lecture25/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 25 March 19,

5

86 88 89 8: 8. 8<...

6 & 8 & 9 & : & . & < & = & > & ; & ? 8=

!

!"

#$%&'!&(&)!**+,-.&'&/#01+2-#$%334

8 9 . ; .

!567&(&84!587&(&94!597&(&.4!5:7&(&;4

D E . F

Page 7: Lecture 25 - Dynamic Graphics Projectjdeber/aps105h/201201/lectures/Lecture25/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 25 March 19,

5

86 88 89 8: 8. 8<...

6 & 8 & 9 & : & . & < & = & > & ; & ? 8=

!

!"

#$%&'!&(&)!**+,-.&'&/#01+2-#$%334

!&(&@1!**+,-!A&<&'&/#01+2-#$%334

8 9 . ; .

!567&(&84!587&(&94!597&(&.4!5:7&(&;4

D E . F

Page 8: Lecture 25 - Dynamic Graphics Projectjdeber/aps105h/201201/lectures/Lecture25/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 25 March 19,

5

86 88 89 8: 8. 8<...

6 & 8 & 9 & : & . & < & = & > & ; & ? 8=

!

!"

#$%&'!&(&)!**+,-.&'&/#01+2-#$%334

!&(&@1!**+,-!A&<&'&/#01+2-#$%334

8 9 . ; .

!567&(&84!587&(&94!597&(&.4!5:7&(&;4

D E . F

Page 9: Lecture 25 - Dynamic Graphics Projectjdeber/aps105h/201201/lectures/Lecture25/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 25 March 19,

5

86 88 89 8: 8. 8<...

6 & 8 & 9 & : & . & < & = & > & ; & ? 8=<

!B

!"

#$%&'!&(&)!**+,-.&'&/#01+2-#$%334

!&(&@1!**+,-!A&<&'&/#01+2-#$%334

8 9 . ; .

!567&(&84!587&(&94!597&(&.4!5:7&(&;4

D E . F

Page 10: Lecture 25 - Dynamic Graphics Projectjdeber/aps105h/201201/lectures/Lecture25/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 25 March 19,

5

86 88 89 8: 8. 8<...

6 & 8 & 9 & : & . & < & = & > & ; & ? 8=<

!B

!"

#$%&'!&(&)!**+,-.&'&/#01+2-#$%334

!&(&@1!**+,-!A&<&'&/#01+2-#$%334

8 9 . ; .

!567&(&84!587&(&94!597&(&.4!5:7&(&;4

D E . F

Page 11: Lecture 25 - Dynamic Graphics Projectjdeber/aps105h/201201/lectures/Lecture25/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 25 March 19,

5

86 88 89 8: 8. 8<...

6 & 8 & 9 & : & . & < & = & > & ; & ? 8=<

!B

!"

#$%&'!&(&)!**+,-.&'&/#01+2-#$%334

!&(&@1!**+,-!A&<&'&/#01+2-#$%334

8 9 . ; .

!567&(&84!587&(&94!597&(&.4!5:7&(&;4

D E . F

Page 12: Lecture 25 - Dynamic Graphics Projectjdeber/aps105h/201201/lectures/Lecture25/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 25 March 19,

5

86 88 89 8: 8. 8<...

6 & 8 & 9 & : & . & < & = & > & ; & ? 8=<

!B

!"

#$%&'!&(&)!**+,-.&'&/#01+2-#$%334

!&(&@1!**+,-!A&<&'&/#01+2-#$%334

8 9 . ; .

!567&(&84!587&(&94!597&(&.4!5:7&(&;4

D E . F

Page 13: Lecture 25 - Dynamic Graphics Projectjdeber/aps105h/201201/lectures/Lecture25/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 25 March 19,

5

86 88 89 8: 8. 8<...

6 & 8 & 9 & : & . & < & = & > & ; & ? 8=<

!B

!"

#$%&'!&(&)!**+,-.&'&/#01+2-#$%334

!&(&@1!**+,-!A&<&'&/#01+2-#$%334

8 9 . ; .

!567&(&84!587&(&94!597&(&.4!5:7&(&;4

D E . F

Page 14: Lecture 25 - Dynamic Graphics Projectjdeber/aps105h/201201/lectures/Lecture25/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 25 March 19,

5

86 88 89 8: 8. 8<...

6 & 8 & 9 & : & . & < & = & > & ; & ? 8=<

!B

!"

#$%&'!&(&)!**+,-.&'&/#01+2-#$%334

!&(&@1!**+,-!A&<&'&/#01+2-#$%334

8 9 . ; G

!567&(&84!587&(&94!597&(&.4!5:7&(&;4

D E . F

Page 15: Lecture 25 - Dynamic Graphics Projectjdeber/aps105h/201201/lectures/Lecture25/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 25 March 19,

6

Updating Pointers

• After a call to !"#$$%&'( every pointer to that chunk of memory must be updated

#,>,!"#$$%&'#6,C,9,1);"%2')*+((0

)*+,9$#!H"1+,>,7#-D/0III

9$#!H"1+,>,DB0 Wrong

$#!H"1+,>,7#-D/0

9$#!H"1+,>,DB0

Page 16: Lecture 25 - Dynamic Graphics Projectjdeber/aps105h/201201/lectures/Lecture25/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 25 March 19,

7

!"#$$%&'( and ?@AA• If !"#$$%&'( can’t resize the memory, it returns ?@AA

• Technically, you should always check for this

• In this course, we won’t

#,>,!"#$$%&'#6,C,9,1);"%2')*+((0# could be ?@AA

)*+,9!"1);"5,>,!"#$$%&'#6,C,9,1);"%2')*+((0)2,'!"1);"5,J>,?@AA(K,,#,>,!"1);"50L"$1"K,,MM,N#*5$",+N","!!%!L

Page 17: Lecture 25 - Dynamic Graphics Projectjdeber/aps105h/201201/lectures/Lecture25/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 25 March 19,

Shortening with !"#$$%&'(• Shortening an array loses everything that’s cut off

8

D E . F DO

#$%&'!&(&)!**+,&-<&'&/#01+2-#$%334

!"

EE&F#**&#$&!#$%&'C&(&D!5874

C"

Page 18: Lecture 25 - Dynamic Graphics Projectjdeber/aps105h/201201/lectures/Lecture25/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 25 March 19,

Shortening with !"#$$%&'(• Shortening an array loses everything that’s cut off

8

D E . F

#$%&'!&(&)!**+,&-<&'&/#01+2-#$%334

!"

EE&F#**&#$&!!&(&@1!**+,-!A&.&'&/#01+2-#$%334

#$%&'C&(&D!5874

C"

Page 19: Lecture 25 - Dynamic Graphics Projectjdeber/aps105h/201201/lectures/Lecture25/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 25 March 19,

Shortening with !"#$$%&'(• Shortening an array loses everything that’s cut off

8

D E . F

#$%&'!&(&)!**+,&-<&'&/#01+2-#$%334

!"

EE&F#**&#$&!!&(&@1!**+,-!A&.&'&/#01+2-#$%334

#$%&'C&(&D!5874

C"

!5.7&(&:94 Wrong

Page 20: Lecture 25 - Dynamic Graphics Projectjdeber/aps105h/201201/lectures/Lecture25/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 25 March 19,

Shortening with !"#$$%&'(• Shortening an array loses everything that’s cut off

8

D E . F

#$%&'!&(&)!**+,&-<&'&/#01+2-#$%334

!"

EE&F#**&#$&!!&(&@1!**+,-!A&.&'&/#01+2-#$%334

#$%&'C&(&D!5874

'C&(&:94

C"

!5.7&(&:94 Wrong

Wrong

Page 21: Lecture 25 - Dynamic Graphics Projectjdeber/aps105h/201201/lectures/Lecture25/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 25 March 19,

Shortening with !"#$$%&'(• Shortening an array loses everything that’s cut off

8

D E . F

#$%&'!&(&)!**+,&-<&'&/#01+2-#$%334

!"

EE&F#**&#$&!!&(&@1!**+,-!A&.&'&/#01+2-#$%334

#$%&'C&(&D!5874

'C&(&:94

C"

!5.7&(&:94 Wrong

Wrong

Page 22: Lecture 25 - Dynamic Graphics Projectjdeber/aps105h/201201/lectures/Lecture25/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 25 March 19,

Shortening with !"#$$%&'(• Shortening an array loses everything that’s cut off

• Still need to update pointers

8

D E . F

#$%&'!&(&)!**+,&-<&'&/#01+2-#$%334

!"

EE&F#**&#$&!!&(&@1!**+,-!A&.&'&/#01+2-#$%334

#$%&'C&(&D!5874

C&(&D!5874'C&(&:94

C"

!5.7&(&:94 Wrong

Wrong

Page 23: Lecture 25 - Dynamic Graphics Projectjdeber/aps105h/201201/lectures/Lecture25/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 25 March 19,

!"#$$%&'( Bugs

• Same as =#$$%&'( and 2!""'(

• !"#$$%&'(ing memory you didn’t get from =#$$%&'(

• Double free by !"#$$%&':6,B( after 2!""':(

• Use after free by !"#$$%&'(ing a 2!""'(ed pointer

• Forgetting to update pointers

9

Page 24: Lecture 25 - Dynamic Graphics Projectjdeber/aps105h/201201/lectures/Lecture25/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 25 March 19,

Commenting

10

Page 25: Lecture 25 - Dynamic Graphics Projectjdeber/aps105h/201201/lectures/Lecture25/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 25 March 19,

Commenting

• Two audiences for your code:

• Compiler

• Other people

• Other people includes future you

11

Page 26: Lecture 25 - Dynamic Graphics Projectjdeber/aps105h/201201/lectures/Lecture25/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 25 March 19,

Compiler• Doesn’t care about identifier names

• Doesn’t care about whitespace

• Doesn’t care about comments

• Cares about syntax

12

)*+,)%$0 )*+,)*5"PQ2A#!H"1+0

)2,')RB(K1S=T>D0L

)2,'),R,B(K,,,1S=,T>,D0L

5%SU$",1);")*+,:%1)+)%*0

5%SU$",1);"0)*+,:%1)+)%*0

5%SU$",#*H$"0MM,V*H$",%2,+N!%W6,)*,!#5)#*1I5%SU$",#*H$"0

)2')RB(K1S=T>D0L

Page 27: Lecture 25 - Dynamic Graphics Projectjdeber/aps105h/201201/lectures/Lecture25/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 25 March 19,

Compiler• Doesn’t care about identifier names

• Doesn’t care about whitespace

• Doesn’t care about comments

• Cares about syntax

12

)*+,)%$0 )*+,)*5"PQ2A#!H"1+0

)2,')RB(K1S=T>D0L

)2,'),R,B(K,,,1S=,T>,D0L

5%SU$",1);")*+,:%1)+)%*0

5%SU$",1);"0)*+,:%1)+)%*0

HumanDoes

Does

Does

Doesn’t care as much5%SU$",#*H$"0

MM,V*H$",%2,+N!%W6,)*,!#5)#*1I5%SU$",#*H$"0

)2')RB(K1S=T>D0L

Page 28: Lecture 25 - Dynamic Graphics Projectjdeber/aps105h/201201/lectures/Lecture25/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 25 March 19,

13

5%SU$",=1,>,BIB0)*+,=*,>,B05%SU$",21,>,BIB0)*+,2*,>,B02%!,')*+,),>,D0,),X>,C0,)TT(K)2,'+-)/,J>,B(K:!)*+2'3Y)=",%2,!#&"!,45,)1,4HZ*36,)6,+-)/(0)2,'),4,E,>>,B(K=1,T>,+-)/0=*TT0L"$1"K21,T>,+-)/02*TT0LLL5%SU$",=#,>,=1,M,=*05%SU$",2#,>,21,M,2*0:!)*+2'3V8"!#H",+)="1,#!",4H,#*5,4HZ*36,=#6,2#(0

5%SU$",+-C/0MM,'#11S=",+,)1,2)$$"5,)*,N"!"(

Page 29: Lecture 25 - Dynamic Graphics Projectjdeber/aps105h/201201/lectures/Lecture25/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 25 March 19,

14

5%SU$",=1,>,BIB0)*+,=*,>,B05%SU$",21,>,BIB0)*+,2*,>,B02%!,')*+,),>,D0,),X>,C0,)TT(K,,)2,'+-)/,J>,B(,,K,,,,:!)*+2'3Y)=",%2,!#&"!,45,)1,4HZ*36,)6,+-)/(0,,,,)2,'),4,E,>>,B(,,,,K,,,,,,=1,T>,+-)/0,,,,,,=*TT0,,,,L,,,,"$1",,,,K,,,,,,21,T>,+-)/0,,,,,,2*TT0,,,,L,,LL5%SU$",=#,>,=1,M,=*05%SU$",2#,>,21,M,2*0:!)*+2'3V8"!#H",+)="1,#!",4H,#*5,4HZ*36,=#6,2#(0

5%SU$",+-C/0MM,'#11S=",+,)1,2)$$"5,)*,N"!"(

Page 30: Lecture 25 - Dynamic Graphics Projectjdeber/aps105h/201201/lectures/Lecture25/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 25 March 19,

5%SU$",=#$"[S=,>,BIB0)*+,*S=\#$"]#&"!1,>,B05%SU$",2"=#$"[S=,>,BIB0)*+,*S=^"=#$"]#&"!1,>,B02%!,')*+,),>,D0,),X>,C0,)TT(K,,)2,'+)="1-)/,J>,B(,,K,,,,:!)*+2'3Y)=",%2,!#&"!,45,)1,4HZ*36,)6,+)="1-)/(0,,,,)2,'),4,E,>>,B(,,,,K,,,,,,=#$"[S=,T>,+)="1-)/0,,,,,,*S=\#$"]#&"!1TT0,,,,L,,,,"$1",,,,K,,,,,,2"=#$"[S=,T>,+)="1-)/0,,,,,,*S=^"=#$"]#&"!1TT0,,,,L,,LL5%SU$",=#$"V8H,>,=#$"[S=,M,*S=\#$"]#&"!105%SU$",2"=#$"V8H,>,2"=#$"[S=,M,*S=^"=#$"]#&"!10:!)*+2'3V8"!#H",+)="1,#!",4H,#*5,4HZ*36,=#$"V8H6,2"=#$"V8H(0

5%SU$",+)="1-C/0MM,'#11S=",+)="1,)1,2)$$"5,)*,N"!"(

Page 31: Lecture 25 - Dynamic Graphics Projectjdeber/aps105h/201201/lectures/Lecture25/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 25 March 19,

5%SU$",=#$"[S=,>,BIB0)*+,*S=\#$"]#&"!1,>,B05%SU$",2"=#$"[S=,>,BIB0)*+,*S=^"=#$"]#&"!1,>,B02%!,')*+,),>,D0,),X>,C0,)TT(K,,)2,'+)="1-)/,J>,B(,,K,,,,:!)*+2'3Y)=",%2,!#&"!,45,)1,4HZ*36,)6,+)="1-)/(0,,,,)2,'),4,E,>>,B(,,,,K,,,,,,=#$"[S=,T>,+)="1-)/0,,,,,,*S=\#$"]#&"!1TT0,,,,L,,,,"$1",,,,K,,,,,,2"=#$"[S=,T>,+)="1-)/0,,,,,,*S=^"=#$"]#&"!1TT0,,,,L,,LL5%SU$",=#$"V8H,>,=#$"[S=,M,*S=\#$"]#&"!105%SU$",2"=#$"V8H,>,2"=#$"[S=,M,*S=^"=#$"]#&"!10:!)*+2'3V8"!#H",+)="1,#!",4H,#*5,4HZ*36,=#$"V8H6,2"=#$"V8H(0

5%SU$",+)="1-C/0MM,'#11S=",+)="1,)1,2)$$"5,)*,N"!"(

2%!,')*+,),>,D0,),X,C0,)TT(_

2%!,')*+,),>,B0,),X,C0,)TT(_

2%!,')*+,),>,B0,),X,.0,)TT(_

Page 32: Lecture 25 - Dynamic Graphics Projectjdeber/aps105h/201201/lectures/Lecture25/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 25 March 19,

16

M9,YN",+%+#$,*S=U"!,%2,!#&"!1I,9M`5"2)*",?@\<]Vab][,.

M9,YN",2)*)1N)*H,+)=",')*,1(,%2,"#&N,!#&"!I,,9,YN",+)=",%2,!#&"!,W)+N,U)U,*S=U"!,*,)1,1+%!"5,)*,9,+)="1-*/I,\#$",!#&"!1,N#8","8"*,U)U,*S=U"!16,2"=#$",9,!#&"!1,N#8",%55,U)U,*S=U"!1I,+)="1,S1"1,DcU#1"5,9,)*5"P)*H6,1)*&",+N"!",)1,*%,U)U,*S=U"!,BI,9,d2,+N"!",)1,*%,+)=",!"&%!5"5,2%!,#,H)8"*,U)U,*S=U"!6,9,+N","*+!e,)1,BIBI,9M5%SU$",+)="1-?@\<]Vab][,T,D/0

M9,YN",1S=,%2,#$$,%2,+N",=#$",!#&"!1f,+)="1I,9M5%SU$",=#$"[S=,>,BIB0,,,,M9,YN",*S=U"!,%2,=#$",!#&"!1,2%S*5,)*,f+)="1fI,9M)*+,*S=\#$"]#&"!1,>,B0

M9,YN",1S=,%2,#$$,%2,+N",2"=#$",!#&"!1f,+)="1I,9M,,,,5%SU$",2"=#$"[S=,>,BIB0

M9,YN",*S=U"!,%2,2"=#$",!#&"!1,2%S*5,)*,f+)="1fI,9M)*+,*S=^"=#$"]#&"!1,>,B0

Page 33: Lecture 25 - Dynamic Graphics Projectjdeber/aps105h/201201/lectures/Lecture25/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 25 March 19,

17

MM,g!%&"11,"#&N,"$"="*+,%2,+)="16,WN)&N,S1"1,DcU#1"5,)*5"P)*H2%!,')*+,),>,D0,),X>,C0,)TT(K,,)2,'+)="1-)/,J>,B(,,K,,,,:!)*+2'3Y)=",%2,!#&"!,45,)1,4HZ*36,)6,+)="1-)/(0,,,,,,,,,,,,,,,,MM,b8"*,U)U1,#!",=#$"6,%55,U)U1,#!",2"=#$",,,,)2,'),4,E,>>,B(,,,,K,,,,,,=#$"[S=,T>,+)="1-)/0,,,,,,*S=\#$"]#&"!1TT0,,,,L,,,,"$1",,,,K,,,,,,2"=#$"[S=,T>,+)="1-)/0,,,,,,*S=^"=#$"]#&"!1TT0,,,,L,,LL

Page 34: Lecture 25 - Dynamic Graphics Projectjdeber/aps105h/201201/lectures/Lecture25/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 25 March 19,

18

MM,g!%&"11,"#&N,"$"="*+,%2,+)="16,WN)&N,S1"1,DcU#1"5,)*5"P)*H2%!,')*+,),>,D0,),X>,?@\<]Vab][0,)TT(K,,)2,'+)="1-)/,J>,B(,,K,,,,:!)*+2'3Y)=",%2,!#&"!,45,)1,4HZ*36,)6,+)="1-)/(0,,,,,,,,,,,,,,,,MM,b8"*,U)U1,#!",=#$"6,%55,U)U1,#!",2"=#$",,,,)2,'),4,E,>>,B(,,,,K,,,,,,=#$"[S=,T>,+)="1-)/0,,,,,,*S=\#$"]#&"!1TT0,,,,L,,,,"$1",,,,K,,,,,,2"=#$"[S=,T>,+)="1-)/0,,,,,,*S=^"=#$"]#&"!1TT0,,,,L,,LL

Page 35: Lecture 25 - Dynamic Graphics Projectjdeber/aps105h/201201/lectures/Lecture25/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 25 March 19,

19

5%SU$",=1,>,BIB0)*+,=*,>,B05%SU$",21,>,BIB0)*+,2*,>,B02%!,')*+,),>,D0,),X>,C0,)TT(K)2,'+-)/,J>,B(K:!)*+2'3Y)=",%2,!#&"!,45,)1,4HZ*36,)6,+-)/(0)2,'),4,E,>>,B(K=1,T>,+-)/0=*TT0L"$1"K21,T>,+-)/02*TT0LLL5%SU$",=#,>,=1,M,=*05%SU$",2#,>,21,M,2*0:!)*+2'3V8"!#H",+)="1,#!",4H,#*5,4HZ*36,=#6,2#(0

5%SU$",+-C/0MM,'#11S=",+,)1,2)$$"5,)*,N"!"(

Page 36: Lecture 25 - Dynamic Graphics Projectjdeber/aps105h/201201/lectures/Lecture25/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 25 March 19,

20

Commenting

• Commenting and style go hand in hand

• Good identifier names helps with commenting

M9,YN",1S=,%2,#$$,%2,+N",=#$",!#&"!1f,+)="1I,9M5%SU$",=#$"[S=,>,BIB0

Page 37: Lecture 25 - Dynamic Graphics Projectjdeber/aps105h/201201/lectures/Lecture25/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 25 March 19,

Assumptions

• Assume the reader knows C

21

)TT0,MM,V55,%*",+%,)

MM,h"":,H%)*H,WN)$",1S=,)1,$"11,+N#*,BWN)$",'1S=,X,B(K,,IIIL

MM,a!"#+",#,8#!)#U$",+%,N%$5,+N",1);")*+,1);"0,

Page 38: Lecture 25 - Dynamic Graphics Projectjdeber/aps105h/201201/lectures/Lecture25/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 25 March 19,

Why, not What

• In general, tell the reader why you’re doing something, not what you’re doing

• Anything out of the ordinary

22

EE&G11C&H+#$H&I$%#*&J1&K!L1&1$+IHK&M!%!&%+&C@+,1//JK#*1&-/I)&N&OPQRSPTUV3W&&XXXY

MM,YN",)+"=f1,1);"6,)*,&=)*+,1);"0,

Page 39: Lecture 25 - Dynamic Graphics Projectjdeber/aps105h/201201/lectures/Lecture25/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 25 March 19,

23

If you were reading this code, what would you want to know?

Page 40: Lecture 25 - Dynamic Graphics Projectjdeber/aps105h/201201/lectures/Lecture25/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 25 March 19,

Testing

24

Page 41: Lecture 25 - Dynamic Graphics Projectjdeber/aps105h/201201/lectures/Lecture25/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 25 March 19,

Error Free

25

• How do we prove that our programs are error free?

Well, we can test them, right?

Page 42: Lecture 25 - Dynamic Graphics Projectjdeber/aps105h/201201/lectures/Lecture25/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 25 March 19,

26

M9,]"+S!*1,+N",1iS#!",%2,f*f,9M)*+,1iS#!"')*+,*(0

)*+,*,>,E0

:!)*+2'345Z*36,1iS#!"'*((0

:!)*+2'345Z*36,1iS#!"'*((0

*,>,.0

:!)*+2'345Z*36,1iS#!"'*((0

*,>,C0

:!)*+2'345Z*36,1iS#!"'*((0

*,>,CE0

.

DO

EC

EjB.

Page 43: Lecture 25 - Dynamic Graphics Projectjdeber/aps105h/201201/lectures/Lecture25/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 25 March 19,

Error Free

27

• How do you prove your programs are error free?

• How do you prove that unicorns do not exist?

• How do you prove that this rock repels tigers?

Hmmm...

Well, we can test them, right?

Page 44: Lecture 25 - Dynamic Graphics Projectjdeber/aps105h/201201/lectures/Lecture25/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 25 March 19,

You Can’t Prove a Negative!

• Basic tenet of logic and science

• All you can say is that something is highly unlikely

• More evidence makes it more unlikely

• Since software is essentially math, you can actually do formal proofs on it

• Very difficult and very expensive

28

Page 45: Lecture 25 - Dynamic Graphics Projectjdeber/aps105h/201201/lectures/Lecture25/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 25 March 19,

29

#$%&/ZI!@1-#$%&$3W&&@1%I@$&$&'&$4Y

#$%&/ZI!@1-#$%&$3W&&#2&-$&((&:;>.<3&&W&&&&#$%&'C&(&_`UU4&&&&'C&(&$4&&Y&&&&&&@1%I@$&$&'&$4Y

E'&Q1%I@$/&%K1&/ZI!@1&+2&[$[&'E#$%&/ZI!@1-#$%&$34

#$%&$&(&94

C@#$%2-\]M^$\A&/ZI!@1-$334

C@#$%2-\]M^$\A&/ZI!@1-$334

$&(&.4

C@#$%2-\]M^$\A&/ZI!@1-$334

$&(&<4

C@#$%2-\]M^$\A&/ZI!@1-$334

$&(&<94

Page 46: Lecture 25 - Dynamic Graphics Projectjdeber/aps105h/201201/lectures/Lecture25/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 25 March 19,

Test Cases

30

• Because we can’t test every possible input, we need to select a representative sample

• A good selection is essential

Page 47: Lecture 25 - Dynamic Graphics Projectjdeber/aps105h/201201/lectures/Lecture25/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 25 March 19,

31

Testing Approaches

• Black-box testing

• White-box testing

• These are general categories, not exact classifications

Page 48: Lecture 25 - Dynamic Graphics Projectjdeber/aps105h/201201/lectures/Lecture25/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 25 March 19,

Black-box

32

E'&Q1%I@$/&%K1&/ZI!@1&+2&[$[&'E

ProgramInput Output

Page 49: Lecture 25 - Dynamic Graphics Projectjdeber/aps105h/201201/lectures/Lecture25/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 25 March 19,

White-box

33

!"#$%&'()*+!"#$",-$$)*#')"$"$.$"/0

Input Output

Page 50: Lecture 25 - Dynamic Graphics Projectjdeber/aps105h/201201/lectures/Lecture25/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 25 March 19,

Test Cases

34

• Boundary cases (a.k.a. edge cases)

• Simplest cases

• General cases

Page 51: Lecture 25 - Dynamic Graphics Projectjdeber/aps105h/201201/lectures/Lecture25/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 25 March 19,

35

M9,]"+S!*1,+N",$#!H"!,%2,f#f,#*5,fUfI,9M)*+,=#P')*+,#6,)*+,U(0

# U What it’s testing Expecteda9 9 !&$1H!%#L1A&b&C+/#%#L1 9: a: !&C+/#%#L1A&b&$1H!%#L1 :6 8 6&!$M&8 88 6 6&!$M&8 8a8 6 6&!$M&a8 66 a8 6&!$M&a8 66 6 b+%K&6 c8 8 b+%K&8 c=< 9: H1$1@!*A&!&d&b =<<=. 98?: H1$1@!*A&!&N&b 98?::;> :;> H1$1@!*A&!&((&b c

Page 52: Lecture 25 - Dynamic Graphics Projectjdeber/aps105h/201201/lectures/Lecture25/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 25 March 19,

36

Special Cases• When dealing with numbers, consider 1, 2 (and 324,

• Other specific cases (e.g., even/odd)

• When dealing with strings, consider empty string, string with one char, multiple null characters (if appropriate)

• When dealing with arrays, consider empty array, array with one item, array with one space, full array

• Need to think about abnormal input, although generally not in this course

Page 53: Lecture 25 - Dynamic Graphics Projectjdeber/aps105h/201201/lectures/Lecture25/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 25 March 19,

Debugging

37

Page 54: Lecture 25 - Dynamic Graphics Projectjdeber/aps105h/201201/lectures/Lecture25/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 25 March 19,

All Software Has Bugs

38

• Unavoidable

• Techniques to help avoid them, and to help find them

Everyone knows that debugging is twice as hard as writing a program in the first place. So if you're as clever as you can be when you write it, how will you ever debug it?

Brian Kernighan

Page 55: Lecture 25 - Dynamic Graphics Projectjdeber/aps105h/201201/lectures/Lecture25/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 25 March 19,

39Image: Naval Historical Center

Grace Hopper’s Lab Notebook

Page 56: Lecture 25 - Dynamic Graphics Projectjdeber/aps105h/201201/lectures/Lecture25/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 25 March 19,

Types of Bugs

• Code that does not do what it’s supposed to

• Code that does what it’s supposed to, but that isn’t the right thing to do

• May be a bug in the design

40