Appendix E. Sample programs for debugging tools

Table of Contents

GDB Example
Valgrind Example

GDB Example

A simple program which contains a simple NULL-indirection bug that can be run under gdb is shown below.

#include "stdio.h"

void
print_scrambled(char *message)
{
  int i = 3;
  do {
    printf("%c", (*message)+i);
  } while (*++message);
  printf("\n");
}

int
main()
{
  char * bad_message = NULL;
  char * good_message = "Hello, world.";

  print_scrambled(good_message);
  print_scrambled(bad_message);
}