Programming Fundamentals
1511 Initial Debugging Guide
First, figure out what kind of error do you have?
- Compile Error → Getting an error straight after you run the command
$ dcc my_code.c -o my_code
- Autotest Error → the autotest is failing
- Run time error → You get an error after running
./my_code
or during - Logic error → Your code compiles and runs, but gets the incorrect answer
Compiling Errors
-
Start from the very top, look for where you wrote
dcc my_code.c -o my_code
, you can ignore every error except the first one for now! -
Read the whole error message! If some of it doesn’t make sense, that’s alright, usually the “
dcc explanation
” part will explain the error in English. -
Look at the line number and character number given, have a look at this line of code in your file and see if you can find your bug with the error message in mind.
-
If you think you’ve fixed the bug, now save your file, recompile and start from step 1 again.
Need more help?
Try running dcc-help
to get further explaination on yoru error message.
Autotest Errors
-
If you have multiple tests failing, pick one of the test that is failing to concentrate on, this could be any of them (but something near the top is probably simpler!). So look for the line that says “failed”, like this:
-
Read through the whole test, and look out for the part that shows the different between your output and the correct output.
-
The red, (anything after the “-” ) is your output, and the green (anything after the “+”) is the expected output:
In this example, the autotest expected “Please enter the number of students and tutors: 5 + 5 = 10” but your program outputted “Enter the number of students and tutors: 5 + 5 = 10”
-
-
You could be getting a runtime error, this might look like this:
If so, then manually run the test case that you were looking at without the autotest, and follow the instructions for runtime errors (below).
-
If you are still confused, then manually run the test case that you were looking at without the autotest. Some things to look out for when doing this are:
-
Are the new lines in the correct spot (compare against the example given in the question description)
-
Is the answer actually the correct answer? Figure out the answer on pen and paper and compare with the autotest output to check.
-
Check that you are typing in the exact same input. Sometimes there is no “\n” at the end of the input, this means that to run the test, you might need to type
[ctrl-d]
instead of[enter] then [ctrl-d]
. Like in the following example:
-
-
Fix the error in the output in your program. Now save your file, rerun the autotest and start from step 1 again.
Runtime Errors
Usually runtime errors mean that you have one of the following errors. Try to figure out which one you have.
-
Accessed a part of an array or string that doesn’t exist (e.g. if an array has 10 slots, then
array[10]
or above will be out of bounds).here is what the dcc error will look like:
-
trying to access uninitialized data. If you haven’t told the program what to put inside of a variable, it will not be able to print it out!
here is what the dcc error will look like:
-
dereferencing a pointer that points to NULL. When dereferencing a pointer, *make sure you have a clear idea of what variable ptr is. Usually somewhere in your code you should have
ptr = &some_variable
orptr = some_function()
.here is what the dcc error will look like:
Some way to fix this are:
-
Making sure that you have no warnings after compiling with dcc. Warnings don’t stop your code from compiling, but they can cause runtime errors when you run the program after compilation. Try not to get into the habit of ignoring warnings.
-
Read the whole error, dcc will point out a line of code and the values of some variables. Check what variables that line of code uses and if any of them are uninitialized.
-
If some values are not what you expect them to be, then you can either
- Think about what you want the values to be, then hand execute your code with pen & paper to see if your expectations and code line up.
- Put in various print statements like the ones below and check that it lines up with your hand execution.
printf("variable1: %d, variable2: %d\n", variable1, variable2);
Logic Error
Logic errors are the hardest to figure out, as there are no error messages to help you. Some steps to help find them are:
- Make sure that your idea of how to solve the problem is correct. This doesn’t require any coding, follow your method with some example inputs using pen and paper and check that you get the correct outcome.
- If you believe that your idea is correct, next try hand executing your code line by line, keeping track of what variables are what with pen and paper. While doing this, use print statements to double check that values are what you expect them to be.
- If you are coding linked lists or arrays, diagrams are the easiest way to debug. Each line of code is usually one change to your diagram (e.g. malloc-ing a node is similar to drawing a circle for a node, free-ing a node is similar to erasing the circle that represents that node).