Lab Assignment--Compile and Debug


Part 1--A Linked List Project

  1. Copy three files using the following command
    cp /net/data/ftp/pub/class/330/CompileDebug/Exercise/* .
  2. Write the Makefile for the Linked List program. Do not forget the options to debug the program later. Define variables for the object files, compiler name, preprocesor, compile and link options. Note that options that are not needed at this time can be defined as null values. eg. CPPFLAGS = . Do not forget the options to debug the program later.
  3. In addition to the targets referred to in the notes, large projects (like the one your class project is becoming) typically have targets for install: and uninstall:. Create these targets.
  4. Run the program. Try inserting numbers into the linked list and printing the resulting list.
  5. Insert a 0 into the list, you will get a segmentation fault (core dump). Remember to remove the core file.
  6. Use gdb to debug the program.
  7. Use strace to show the system call in your program.

Part 2--Memory Errors

It it important to avoid memory errors when you are writing programs. This C++ memory corruption and memory leaks tutorial is a great overview of the topic. Because many memory errors don't show up immediately or don't apply to certain compilers you may not notice them. The valgrind tool is your memory error finding friend.

Use valgrind as shown in the notes to find the memory errors in your Parsing Line Input code from last week, then try to fix them all so that valgrind reports a clean bill of health:

venus[1]: g++ -ggdb -c oneline.cpp
venus[2]: g++ -ggdb -c main.cpp
venus[3]: g++ -ggdb oneline.o main.o -o tokenizer
venus[4]: valgrind --leak-check=yes tokenizer > output
==9759== Memcheck, a memory error detector
==9759== Copyright (C) 2002-2009, and GNU GPL'd, by Julian Seward et al.
==9759== Using Valgrind-3.5.0 and LibVEX; rerun with -h for copyright info
==9759== Command: tokenizer
==9759== 
==9759== 
==9759== HEAP SUMMARY:
==9759==     in use at exit: 0 bytes in 0 blocks
==9759==   total heap usage: 148 allocs, 148 frees, 12,210 bytes allocated
==9759== 
==9759== All heap blocks were freed -- no leaks are possible
==9759== 
==9759== For counts of detected and suppressed errors, rerun with: -v
==9759== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 4 from 4)
venus[5]: 

Script a compile and valgrind test of your code.

Deliverables: