Homework 2 - Pointers, Memory Management, Debugging



Due: Wednesday 07/24/2002 in class

Submission:

Send by email to crisn@cs.jhu.edu, a file (hw2_name.tar.gz) containing the working directory. Please make sure that you do not include binaries or core files in the tar file.

To create a tar.gz archive, suppose the working directory is hw2_jdoe:

cd ..

tar zcvf - hw2_jdoe > hw2_jdoe.tar.gz

To extract the content of a tar.gz archive, suppose the file is hw2_jdoe.tar.gz:

tar zxvf hw2_jdoe.tar.gz



Homework:


Part 1: Learn how to use the dmalloc library


  1. Compile and make sure the dmalloc library is running on your system:

  2. Download from www.dmalloc.org the latest version

  3. Read the INSTALL file for instructions:

    alias dmalloc 'eval `\dmalloc -C \!*`'

    or in your .bashrc if you are using bash

    function dmalloc { eval `command dmalloc -b $*`; }

    1. Write a simple program to experiment with some memory bugs (play with them one by one): create the bug, run the program and notice what happens in the following cases.

        1. Create a memory leak (you allocate memory and you do not free it)

        2. Free something twice (allocate something dynamically and call free twice)

        3. Free something that was not allocated (declare a pointer and free it without calling malloc first)

        4. Write in a memory area that was not allocated (allocate an array and write outside the array).


    Use dmalloc for the above errors: link your program with DMALLOC library (set the path in LDFLAGS and link by adding -ldmalloc to LIBS). Include dmalloc.h in each c file to get line information about possible memory problems, using

    #ifdef USE_DMALLOC
    #include<dmalloc.h>
    #endif


    You can set the log file as following: dmalloc -l my_logfile -i 100 low

    !!! This is valid for the terminal you run the command. You need to run the program in this terminal. If you move to another terminal, do not forget to set the logfile as above, otherwise dmalloc will not create the log file.


    Pay attention that running the program multiple times will overwrite the dmalloc log file.


    For more information check the INSTALL file that comes with the distribution (section Getting Started with the Library).

    For this part of the assignment submit: Makefile, source code for program with memory bugs (4 files, one for each type of error) and the dmalloc log file (4 log files, one for each type of bug).


    Part 2: Dynamic data structures:

    1. Write a program that will multiply and add two matrices having N lines and N rows.

        1. The program takes as a parameter the size of the matrices (N), you will use it to dynamically allocate memory and to read the matrices. The program should be called like: matrix_op -n N where N is the size of the NxN matrix.

        2. The matrices are read from two different files having the names matrix_A.in and matrix_B.in. Each row is on a different line, columns are separated by one space.

        3. The program will dynamically allocate space for the two matrices A and B read from files, and for other two matrices C and D. C the sum of A and B (C = A + B), D is the multiplication of A and B (D=AB).

        4. Print matrices C and D in two different files: matrix_C.out and matrix_D.out


    For this part of the assignment submit: Makefile, source code for program, and matrix_A.in and matrix_B.in files that you tested your program with.


    For more information see lecture 3 and 4 slides, www.cnds.jhu.edu/courses/cs111 and www.dmalloc.org.