G52CFJ CC for Java Programmers Demo for Lectures 5 and 6 - PowerPoint PPT Presentation

1 / 29
About This Presentation
Title:

G52CFJ CC for Java Programmers Demo for Lectures 5 and 6

Description:

Not matching the printf parameter types with the letters in the control string (e.g. %s, %c) ... e.g. I always double-check switches (because I know I forget break's ... – PowerPoint PPT presentation

Number of Views:41
Avg rating:3.0/5.0
Slides: 30
Provided by: csNo
Category:

less

Transcript and Presenter's Notes

Title: G52CFJ CC for Java Programmers Demo for Lectures 5 and 6


1
G52CFJ C/C for Java Programmers Demo for
Lectures 5 and 6
  • Debugging
  • Stack vs Heap
  • Zombies

2
Contents
  • Simple debugging using printf()
  • Creating your own arrays of strings
  • From user input
  • How NOT to create arrays of struct pointers
  • Zombies sample

3
Debugging
  • A few slides for reference

4
Compiler and linker errors
  • Will differ between compilers
  • For gcc you may want to read
  • http//www.network-theory.co.uk/docs/
  • gccintro/index.html
  • For compiler error messages
  • http//www.network-theory.co.uk/docs/
  • gccintro/gccintro_94.html
  • For linker error messages
  • http//www.network-theory.co.uk/docs/
  • gccintro/gccintro_95.html
  • For runtime errors
  • http//www.network-theory.co.uk/docs/
  • gccintro/gccintro_96.html

5
Common compilation problems
  • Not matching and
  • This is why I line them up on separate lines
  • Not matching if / ifdef / ifndef / else and
    endif
  • Using something before it has been declared
  • E.g. a type or function
  • Did you remember to include the header file?
  • Typo, or missing a
  • Usually gives a parse error/syntax error?
  • Trying to initialise from something which is not
    a constant (or a non-constant array size)

6
Common code errors
  • These wont necessarily crash
  • May give a warning, often do not give a
    compilation error!
  • vs (assignment vs equivalence)
  • Missing breaks from switch statements
  • vs , vs (bitwise vs logical operators)
  • Not matching the printf parameter types with the
    letters in the control string (e.g. s, c)
  • Single vs double quotes a vs a
  • Should get a warning probably about pointer vs
    integer
  • Write out of bounds
  • Over the end or beginning of an array
  • E.g. array size too small, or bad index value
  • Dereference a bad pointer (e.g. memory freed)

7
Tips
  • Unit testing
  • Test functions independently
  • Test groups of functions together
  • Rather than whole program together
  • Look for errors that you know you commonly make
  • e.g. I always double-check switches (because I
    know I forget breaks
  • A handy tip is to always comment when a break is
    deliberately missing
  • Check if statements, ensure there is a double
    equals not a single equals, unless deliberate
  • If deliberate then add a comment!

8
Catching runtime crashes
9
Debugging runtime problems
  • Find out where the problem is
  • Be able to reproduce the error
  • So that you can test the fix
  • Find out what the problem is
  • Fix the problem
  • Test the fix
  • Look for similar problems elsewhere
  • If you made the mistake once it could be
    elsewhere too

10
printf() debugging
  • Scatter calls to printf() throughout your code
  • You could define a debug function
  • printf() in debug, vanishes in release
  • See zombies code
  • printf() a checkpoint string
  • Like the program announcing I got here
  • So you know where it crashed
  • printf() to see the value of a variable
  • To see why it crashed
  • This is a simple solution
  • But, you have to recompile and re-execute each
    time you add more printf()s

11
printf() as a progress point
  • Example of checkpoint code
  • char c h
  • printf( Checkpoint 3\n )
  • printf( s, c )
  • printf( Checkpoint 4\n )
  • Prints Checkpoint 3 but then crashes.
  • You know the error is between the two checkpoint
    printfs

12
printf() to show variable value
  • Bugs are usually for one of two reasons
  • The code was wrong
  • The data provided to the code was wrong
  • In the previous case, the code was wrong
    printf( s, c )
  • You can also use printf debugging to identify
    when data is wrong
  • Once you find out what went wrong, print the
    values of variables prior to executing the
    statement which went wrong

13
Storing strings and structs
14
Accepting a string from the user
  • / Macro for string length /
  • define MAX_STRING_LENGTH 50
  • / A simple function to get a string from the
    user. /
  • char getString()
  • char StringMAX_STRING_LENGTH1 /Allow for 0
    at end/
  • int i -1
  • do
  • / Check that we do not overrun the string
    length /
  • if ( i gt MAX_STRING_LENGTH )
  • break / Stop the do-while loop /
  • Stringi getchar()
  • while ( (Stringi ! '\n') (Stringi !
    EOF) )
  • Stringi '\0' / Ensure zero-termination of
    string /
  • return String / Return the string that was
    loaded /

15
Asking for and storing strings
  • / Main function - asks for 3 strings, then shows
    them. /
  • int main( int argc, char argv )
  • / Array of chars, i.e. array of pointers /
  • char myinputstringsNUMBER_STRINGS
  • int i
  • / Ask for some strings and display them /
  • for ( i 0 i lt NUMBER_STRINGS i )
  • printf( "Please specify string d ", i )
  • myinputstringsi getString()
  • printf( "\nString d was 's'\n", i,
    myinputstringsi )
  • printf( "\nThank you, all strings loaded. Press
    ENTER\n" )
  • while ( getchar() ! '\n' ) / Skip chars until
    ENTER /
  • for ( i 0 i lt NUMBER_STRINGS i )

16
Improvement static local
  • char getString()
  • static char StringMAX_STRING_LENGTH1
  • int i -1
  • do
  • / Check that we do not overrun the string
    length /
  • if ( i gt MAX_STRING_LENGTH )
  • break / Stop the do-while loop /
  • Stringi getchar()
  • while ( (Stringi ! '\n') (Stringi !
    EOF) )
  • / Ensure zero-termination of string /
  • Stringi '\0'
  • / Return the string that was loaded /
  • return String

17
Improvement use malloc()
  • char getString()
  • char String malloc(MAX_STRING_LENGTH)
  • int i -1
  • do
  • if ( i gt MAX_STRING_LENGTH )
  • break / Stop the do-while loop /
  • Stringi getchar()
  • while ( (Stringi ! '\n') (Stringi !
    EOF) )
  • / Ensure zero-termination of string /
  • Stringi '\0'
  • return String
  • / Since memory was malloc'd it must be freed by
    caller /
  • for ( i 0 i lt NUMBER_STRINGS i )
  • free( myinputstringsi )

18
Or get caller to allocate the memory
  • char getString( char String, int iBufferLength)
  • int i -1
  • do
  • if ( i gt (iBufferLength-1) )
  • break
  • Stringi getchar()
  • while((Stringi ! '\n') (Stringi !
    EOF))
  • Stringi '\0'
  • return String
  • myinputstringsi malloc( MAX_STRING_LENGTH 1
    )
  • getString(myinputstringsi, MAX_STRING_LENGTH
    1)
  • And also free() it again, like before

19
Creating structs a problem!
  • MyStruct structsNUMBER_STRUCTS
  • int i
  • srand(1000) / Ensure same results every time
    executed./
  • for ( i 0 i lt NUMBER_STRUCTS i )
  • MyStruct a_new_struct / Create a new struct /
  • / Fill the members of the struct /
  • a_new_struct.v1 rand()
  • a_new_struct.v2 rand()
  • / Store the struct in the array /
  • structsi a_new_struct
  • for ( i 0 i lt NUMBER_STRUCTS i )
  • printf( "\nStruct d has random values d and
    d\n\n",
  • i, structsi-gtv1, structsi-gtv2 )

20
Games, puzzles,
  • (And other programs which have user interaction)

21
Interactive program structure
Initialisation Allocate any resources Create any
windows Request memory allocations
Main loop / game loopDisplay state Request
Input Accept/handle Input Update state
De-initialisation Close any windows Release any
resources Free any memory allocations
22
Zombies
  • How good/bad is the code?
  • How would you improve it?

23
Key elements of Zombies
  • defines for constants
  • Optional display for zombie i
  • define ZOMBIE 'Z'
  • or define ZOMBIE ('a'i)
  • Removal of printf when not in debug using a macro
  • define debug2(a,b,c) printf(a,b,c)
  • or define debug2(a,b,c)
  • Use of srand() and rand()
  • Another malloc() and free() example

24
Questions to consider
  • You should try commenting out parts of the code
    to see what they do, or changing the defines at
    the top
  • What happens if the size is changed?
  • Can the number of zombies and pits be changed?
  • What happens if someone enters too much input?
  • e.g. multiple numbers/letters
  • Why do the zombies and player need to be
    undrawn?
  • What memory needs to be freed?
  • How do the zombies home in on the player
  • How does the code control their movement

25
Zombies heading for player
  • X and Y coordinates kept separately
  • Home in
  • if ( zombie_x gt player_x ) zombie_x--
  • if ( zombie_x lt player_x ) zombie_x
  • if ( zombie_y gt player_y ) zombie_y--
  • if ( zombie_y lt player_y ) zombie_y

26
Moving zombies/player
  • When moving a zombie/player
  • Undraw it at the old position
  • Need to put the empty map position back again
  • Otherwise you get a trail of Zs or Ps on the
    map
  • Try it yourself
  • Move it
  • Update the drawing positions of zombies/player
  • Draw it at the new position
  • Use the new drawing positions to draw

27
Good or bad design?
  • Does a function do multiple different things?
  • Yes? If so then split it up
  • Zombies is an example of a function which is far
    too big
  • Where should I store my data?
  • Currently all data is in local variables
  • If data is used in multiple functions then it
    will need to be passed around, or made global
    (more options in C)
  • Can I group data
  • For zombies, could produce a struct for a
    position (x,y) and previous contents of map
    location, grouping data
  • Then have one struct per player/zombie

28
Exercise
  • Consider the zombies example code
  • You should now be able to work through it
  • Make sure that you understand it
  • Criticise it
  • Is it a good design?
  • Is it easy to understand?
  • Is the structure nice?
  • How could you improve it?
  • Do this now it will help with the coursework!

29
Next lecture (Friday 3pm)(LT2)
  • Now (at 11am)
  • Lab session (to try things yourself)
  • in A25 (Computer Science Lab)
  • With another lab session on
  • Tuesday at 1pm
Write a Comment
User Comments (0)
About PowerShow.com