\documentclass{article}
\usepackage[dvips]{graphics,color}
\usepackage{pifont,a3,lscape,multicol,html,palatino}
% Run latex, then do
% dvips -oposter.ps -t a3 poster.dvi
% to produce an A3 postscript file that you can see by doing
% ghostview -landscape -magstep -3 poster.ps
\makeatletter
\newenvironment{tablehere}
{\def\@captype{table}}
{}
\newenvironment{figurehere}
{\def\@captype{figure}}
{}
\makeatother
\begin{document}
\newenvironment{coldinglist}[2]%
{\begin{list}{\textcolor{#2}{\ding{#1}}}{}}%
{\end{list}}%
\pagestyle{empty}
\begin{landscape}
\begin{center}
\resizebox{25cm}{!}{Cambridge University Engineering Department}
\end{center}
\begin{multicols}{4}
\title{Debugging Programs\\
in Pascal, C, C++ and Fortran}
\author{Tim Love}
\date{\today}
\maketitle
\thispagestyle{empty}
\begin{figurehere}
\resizebox{6cm}{!}{\includegraphics{/users0/tpl/doc/latex/Masters/dde.eps}}
\end{figurehere}
\section*{\textcolor{blue}{Introduction}}
It's not uncommon for over 50\% of programming time to be spent of
debugging.
This document looks at various techniques and utilities (particularly
\texttt{dde}) that aid debugging. It will be useful to you if your
program compiles but doesn't do what you expect when you run it.
\section{\colorbox{red}{Crashes}}
If a program runs into serious problems it may crash, giving a
\texttt{Core Dumped} message. This means that a big file called \texttt{core}
will be created in the directory from where you ran the program. You
may be able to run a post-mortem on the file. Some common
run-time errors are
\begin{itemize}
\item
\texttt{Segmentation Violation}, \texttt{Memory Fault} or
\texttt{Data Memory Protection Trap} -
You are accessing a forbidden part of memory. Perhaps an array
index is beyond bounds or there's a parameter mismatch when a
subroutine's called.
\item
\texttt{Bus Error} -
This usually means that you are accessing misaligned data, but can
also be caused by mis-assigned variables.
\item
\texttt{Floating Exception} -
Division by zero or values which are too big or small will cause
this message to appear.
\end{itemize}
Your program may also crash if it unexpectedly hits a limit of some kind.
\begin{coldinglist}{42}{red}
\item Many systems limit the amount of disc space you can use. Sometimes,
even if you are under quota, the disc is full. In such situations
writing to, or creating, a file will fail. Programs should be written
so that they can cope with these situations.
\item Some systems impose a limit on the size a program can grow to.
On our systems that limit is at least 48Meg. Some programs (Matlab, for
example) can reach this limit. Run \texttt{top} in a separate window
to monitor the size of your program if you think it might be reaching
this limit.
\end{coldinglist}
The resulting core file can be investigated
using \texttt{dde} (see below). The \texttt{adb} program can be used
even if you haven't compiled using the `\verb|-g|'
flag. If a program called \verb|testing| crashes, typing
\texttt{adb testing}, then \verb|$c| will give a backtrace of routines called, which
may help you localize the bug. Get out of \texttt{adb} using \verb|$q|.
\section{Causes and Solutions}
Each language has its own commonly made errors. See the language sections
of this document for details. Some general points are
\begin{itemize}
\item Let the compiler help you as much as possible! Many compilers generate
warning messages if you ask them to.
\item Assume when you're writing your program that it won't be bug-free.
Write it so that
you (and others) can easily debug it: make routines small and control
flow simple.
\item Write ``defensive'' code. Always assume the worst.
\item It's not uncommon for beginners to prove to themselves that their
code is correct. Add print statements to print out intermediate values
and test assertions.
\item If you're using floating point arithmetic, check divisions to see
if you're dividing by zero.
\item Check that you're not going off the end of arrays.
\end{itemize}
\section{Using dde}
\texttt{dde} is a debugger for C, C++, Pascal and Fortran code that give you
control
over execution of a program, allowing you to step through a program
line by line and check the values of variables. The program has built-in
help. The \texttt{dde} commands are the same whichever of the languages the suspect program is
written in, but for sake of argument, a \texttt{Pascal} program is shown.
To try it out, type
\begin{verbatim}
cp -r /opt/dde/examples dde
cd dde
\end{verbatim}
This will copy some example programs into a directory called \texttt{dde},
and move you into it. The pascal version of the program is as follows
\begin{small}
\begin{figurehere}
\begin{verbatim}
{ For the HP/DDE debugger. }
program average (input, output);
const
fi = 1;
la = 10;
type
list_t = array [fi..la] of integer;
const
start_list = list_t [3,4,3,6,7];
var
my_list : list_t;
function sum (list : list_t; ....);
var s, i : integer;
begin
s := 0;
for i := low to high do
s := s + list[i];
sum := s;
end; { sum }
\end{verbatim}
\end{figurehere}
\end{small}
Mail \texttt{pkc@eng} (Pete Clarkson) if you have problems.
\end{multicols}
\end{landscape}
\end{document}