UC3M

Telematic/Audiovisual Syst./Communication Syst. Engineering

Systems Architecture

September 2017 - January 2018

16.9.2.  Debugging a C program

Resources

  • Folder Music_data in your Subversion shared space. There is a local copy here.

Work Plan

The my_music.c program stores information about a collection of disks as it is shown in the following figure:

The global vairables rap, rock, and rumba are structures of type Category with the number of disks stored as a linked list. Each disk is a structure with the number of songs, an array of information about each song and the pointer to the next disk. For each song, its duration and a pointer to its title is stored.

The program (see main function) first prints the number of disks for each category, then the duration of all songs in a category (with the function Duration), and finally the title of the last song of each disk in each category with the function print_title_last_track_in_each_disk.

You may ignore the static functions in the file. They are correctly implemented and they only load and unload the data structures.

  1. Compile and execute the program my_music.c (remember compile it with option -g). It has three errors. Due to them, the messages printed on the screen are incorrect. For example, the duration of the disks in the rap category is incorrect, and the messages showing the title of the last track of every disk are also incorrect.

  2. Using the debugger, analyze its execution and fix it (maybe it is convenient for you to copy the program file to keep also the original one). For that, you can locate the three global variables and the two functions Duration and print_title_last_track_in_each_disk. Execute the programa with the debugger until the point where the duration of the rap category is printed; at this point, execute step by step this calculation. Repeat the procedure for the first call to the function print_title_last_track_in_each_disk.

    Remember the static functions in the file are correctly implemented and they only load and unload the data structures.

  3. The way to know if the execution is correct is because the program output must be (pay attention to the last line):

    --- Welcome to the Music Statistics program ---
        Analyzing your music collection...
    
    2 disks in the rumba category
    2 disks in the rock category
    1 disks in the rap category
    
    Duration of rumba songs: 6019
    Duration of rock songs: 7044
    Duration of rap songs: 4639
    Duration of all songs: 17702
    
    Title of last track in rumba disks:
      Disk 1: vestido de flores
      Disk 2: nina
    
    Title of last track in rock disks:
      Disk 1: god save the queen
      Disk 2: now im here
    
    Title of last track in rap disks:
      Disk 1: curtains close skit
    
     --- PROGRAM IS FIXED! COMMIT YOUR CHANGES ---

Assessment

Optionally, upload the code to the SVN and show it to your instructor. It is recommended for you to note down the steps you have given during the exercise. Later, you will be able to explain your instructor those steps and how the debugger has helped you to find the solution. Your instructor also may check if you understood the use of the debugger with some questions.