UC3M

Telematic/Audiovisual Syst./Communication Syst. Engineering

Systems Architecture

September 2015 - January 2016

Detecting memory leaks and debugging

Lab activities

1.  Debugging a C program

Readings

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.

How long did this activity take you? mins.

2.  Detecting anomalies with Valgrind

Work Plan

In this activity, you will execute files with errors, with Valgrind. These files are under your repository in the Valgrind_errors directory. There is a local copy here. For each of the following files, compile it with the -g directive (that is gcc -Wall -g yourfile.c -o yourexecutable), execute it with Valgrind (valgrind --leak-check=yes ./yourexecutable), and fix the reported errors, explaining in the same file (with a comment of 1-2 lines) the reason for the error:

  1. File strcpy_exercise.c: copies one string over a second one.

  2. File printing_exercise.c: copies an integer to a pointer and prints its address and the value just copied.

Assessment

Opcionalmente, upload the modified files to your repository and show the modifications to the teacher to be evaluated.

How long did this activity take you? mins.

Using arrays of pointers

Resources

Work Plan

Using the program with options to show information of wireless networks, mentioned functions, make the following modifications:

  1. Change the definition of the array that stores the wireless networks in the main function. Each element of the array should be a pointer to a data of type struct ap_scan_info.

  2. Adapt the functions of the program (array_load and others) to support the new type of the elements in the array.

Optionally, upload the file to the repository with svn commit.

How long did this activity take you? mins.

Sorting and removing

Work Plan

Now, it is time for enhacing functionality you have in your Wifi menu with two additional functionalities:

  1. Function number [5] in charge of showing best Wifis. This menu function will show Wifis in increasing quality order.

  2. [6] Delete function in charge of deleting a Wifi record. After setting an Wifi as not available, it will never show up again.

How long did this activity take you? mins.