next up previous
Next: Isolating the bug Up: Debugging a compiled Previous: Producing debug-output

Using assertions

Another possibility is to insert assertions in your code that check if the assumptions of your code are valid. An example:

#include <assert.h>

int mydivision(int numerator, int divisor)
{
    assert(divisor != 0);

    return numerator / divisor;
}
If divisor is not equal to 0, the assert will do nothing, but when divisor is 0 for some reason, the program will halt with a message like:
assertion.cc:8: failed assertion `divisor != 0'
IOT trap - core dumped
This can be very useful while debugging, but asserting every assumption in your code will require a huge amount of asserts and furthermore, it can be argued that it will not result in robust programs; a program that halts as soon as something is wrong can not be said to be very robust.



SE Praktikum
Tue Aug 13 11:13:23 MET DST 1996