lecture 25 - dynamic graphics projectjdeber/aps105h/201201/lectures/lecture25/a… · aps105 winter...
Post on 03-Aug-2020
2 Views
Preview:
TRANSCRIPT
APS105Winter 2012
Jonathan Deberjdeber -at- cs -dot- toronto -dot- edu
Lecture 25March 19, 2012
2
Today
• !"#$$%&'(
• Commenting
• Testing
• Debugging
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
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
!"#$$%&'(
• 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
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
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
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
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
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
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
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
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
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
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
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
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"
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"
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
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
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
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
!"#$$%&'( 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
Commenting
10
Commenting
• Two audiences for your code:
• Compiler
• Other people
• Other people includes future you
11
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
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
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"!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*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"!"(
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
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,
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,
23
If you were reading this code, what would you want to know?
Testing
24
Error Free
25
• How do we prove that our programs are error free?
Well, we can test them, right?
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.
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?
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
29
#$%&/ZI!@1-#$%&$3W&&@1%I@$&$&'&$4Y
#$%&/ZI!@1-#$%&$3W&&-$&((&:;>.<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
Test Cases
30
• Because we can’t test every possible input, we need to select a representative sample
• A good selection is essential
31
Testing Approaches
• Black-box testing
• White-box testing
• These are general categories, not exact classifications
Black-box
32
E'&Q1%I@$/&%K1&/ZI!@1&+2&[$[&'E
ProgramInput Output
White-box
33
!"#$%&'()*+!"#$",-$$)*#')"$"$.$"/0
Input Output
Test Cases
34
• Boundary cases (a.k.a. edge cases)
• Simplest cases
• General cases
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
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
Debugging
37
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
39Image: Naval Historical Center
Grace Hopper’s Lab Notebook
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
top related