Skip main navigation

Introduction to Week 3

Watch Geert Jan Bex presenting the GDB debugger and Valgrind tool as well as some general debugging techniques
We covered best practices for writing clean code, using compilers to catch bugs early, and writing good quality tests. However, you still have to find and fix bugs. Let’s face it, programming is hard. Algorithms and data structures are complex and thinking in advance of all possible edge and corner cases is very difficult. So your code will contain bugs. Typically, a developer spends half of the time finding and fixing bugs. You don’t want to be such a developer, you want to minimize the time you spend debugging. When trying to find bugs, you write print-statements in your code in order to figure out what your programme is doing, which statements are executed, what values of certain variables are.
And if you do it systematically, it works. However, it’s kind of time consuming and a bit error prone since you have to remove those print-statements later and you can introduce additional bugs doing that. So, using a debugger is actually a lot more efficient. For one thing, you don’t have to modify your source code in order to debug it. You can simply run your application step by step, statement by statement, and you can inspect the value of any variable at any given time. You can also run your code up to the point where a Boolean condition is satisfied or certain variable changes and then have a look around at what’s going on.
GDB and its advanced features will be discussed here as well as best practices which you can apply to any debugging tool. No doubt, you’ve come across the dreaded segmentation fault and you know that it’s notoriously difficult to pinpoint and fix the causes of that. Valgrind is a tool that helps you detect memory related problems and helps you fix segmentation faults more easily. Also, it will detect memory leaks to improve the quality of your code. So, essentially this week’s section is about catching bugs out there in the wild.

Week 3 learning goals

During this week, you’ll learn about

  • Finding bugs in serial code using a debugger,
  • Advanced debugger features,
  • Best practices for debugging code,
  • Verifying code at runtime for memory leaks and data access issues.
This article is from the free online

Defensive Programming and Debugging

Created by
FutureLearn - Learning For Life

Our purpose is to transform access to education.

We offer a diverse selection of courses from leading universities and cultural institutions from around the world. These are delivered one step at a time, and are accessible on mobile, tablet and desktop, so you can fit learning around your life.

We believe learning should be an enjoyable, social experience, so our courses offer the opportunity to discuss what you’re learning with others as you go, helping you make fresh discoveries and form new ideas.
You can unlock new opportunities with unlimited access to hundreds of online short courses for a year by subscribing to our Unlimited package. Build your knowledge with top universities and organisations.

Learn more about how FutureLearn is transforming access to education