Lab Assignment--Compile and Debug
Lab Code
To get the sample and exercise code, please use the following commands in your Lab 2 folder:
wget www.labs.cs.uregina.ca/330/CompileDebug/Lab2.zip
unzip Lab2.zip
Part 1 - Makefile
Use the code in the replit teams project
There is a makefile already in the project. Of note are:
- it uses gcc instead of g++, which means that you will be writing pure C code
- it provides the options to debug the program later. Any guesses what you will be doing?
Modify the makefile to:
- target this exercises's C files
- include at least one variable (Not compile, will be lab demo
;
- include a valgrind target so that you can type "make valgrind"
Part 2 - Fix the Code
The code provided should compile; but it contains several logic errors.
Use gdb and valgrind to help you find these errors.
In the end, when you run your code through valgrind, with the following command:
valgrind --leak-check=yes ./your_executable
you should see a
message similar to the following:
==5583== HEAP SUMMARY:
==5583== in use at exit: 0 bytes in 0 blocks
==5583== total heap usage: 1 allocs, 1 frees, 400 bytes allocated
==5583==
==5583== All heap blocks were freed -- no leaks are possible
==5583==
==5583== For counts of detected and suppressed errors, rerun with: -v
==5583== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 8 from 6)
Part 3 - Capture: "make", "valgrind", and "gdb"
In this part, you will bring together all of the tools from this lab
into one script. Follow the steps below
using your debugged code from this lab:
- script scriptname (use your own "scriptname")
- make clean
- make valgrind (this should compile the code and run valgrind on it)
- gdb executable (substitute your own executable name)
- Set a breakpoint on the findMax function (in the
functions.c file)
- run the code with an input of 50 for the size of the array
- Using length, print all of the elements in the array
- print the value stored at max's address
- use next as many times as needed (inside the "for" loop) to see the value stored at max's address change
- After the value stored at max's address has been modified inside the "for" loop, print the value stored at max's address
- Use until at the "for" loop to skip over the other
iterations of the loop
- print the value stored at max's address
- use next to continue through the code until you get a
message that your program or process "exited normally"
- quit the debugger
- exit the script
Deliverables in URCourses: 3 code files, 1 makefile, 1 script file
- Debugged code: main.c, functions.c,
and functions.h
- Makefile for the project.
- with at least one variable and valgrind target
- when submitting, please rename as makefile.txt
- Script of make, valgrind,
and gdb from Part 3 (named with a .txt extension)
Notes
- Do not create a zip file
- Submission is on UR Courses
- Submit your own work
- Be mindful that your assignment is due two hours before the start of your next lab