Project 0: Compiling & Debugging on GL

Due: Tuesday, February 13, 8:59:59pm

Note: there are no late submission folders for Project 0. Do this one on time.

Objectives

The objective of this programming assignment is to make sure that you are set up to compile and debug C++ programs on GL and that you can copy programs into your submission folders.


Introduction

In this project, you will go through all of the mechanical steps of compiling, debugging and submitting a C++ program on GL. If you have submitted programs on GL using shared directories (instead of the submit command), then these steps should be quite familiar.


Assignment

Step 0: familiarize yourself with Unix

If you haven't used Unix before, go over the Unix guides listed in the Resources page.

Step 1: remember gdb

If you do not remember how to use the gdb debugger or never used it at all, then read this tutorial on debugging with gdb. (Yes, do actually read it.)

Next, complete the Spring 2016 CMSC 202 gdb lab. (Yes, actually go through the steps of this lab.)

Step 2: link your shared directory

Follow the instructions on the Project Submission page to make a symbolic link to the shared directory in your home directory.


Step 3: copy the following program to your proj0 submission directory.

// File: proj0.cpp // // CMSC 341 Spring 2018 Project 0 // // Program to print out a randomized sequence of the // numbers from 0 to 9, Using a really dumb algorithm. // // Why does this program segfault??!?!?!?! // (You are NOT supposed to actually fix this code, though!!!) // #include <cstdlib> #include <string> #include <iostream> using namespace std; main() { string *str[10]; int num_left; int pick; string *sptr; // Fill array with freshly-constructed strings for (int i = 0; i < 10; i++) { str[i] = new string(1, static_cast<char>('0' + i)); num_left++; } // Now, scramble by picking a slot at random, making sure it's not // already been processed, then printing, deleting and nulling out while (num_left > 0) { pick = rand() % 10; sptr = str[pick]; if (sptr = NULL) { // NULL indicates we already processed this; continue; // skip and continue looping } cout << *str[pick] << endl; delete str[pick]; str[pick] = NULL; --num_left; } }

The file is also available on GL as:

/afs/umbc.edu/users/p/a/park/pub/cmsc341/s18/proj0/proj0.cpp

Step 4: Compile and run

Go to your proj0 submission directory. If you followed the recommended setup, you would use the Unix command:

cd ~/cs341proj/proj0

Your copy of the proj0.cpp file should be in this directory. (Check using ls.) Then, compile using this exact Unix command (just cut and paste):

g++ -gstabs+ proj0.cpp -o proj0.out

The command option "-gstabs+" tells g++ to create debugging information that in the stabs format using extensions that is understood only by the gdb debugger. I.e., it tells g++ to play nice with gdb.

If your files do not compile using the specified Unix command, you will receive a very low grade.

Now run the compiled program:

./proj0.out

It should print out a few numbers, then crash with "Segmentation fault" error; this should feel very familiar from your CMSC 202 days. You do not have to actually fix the code: just find out where and why it crashed.


Step 5: Debug the program using gdb

Use gdb to perform the following steps. (You can do this because you actually read the tutorial and worked on the lab in Step 1.)


Step 6: record yourself compiling and debugging

Note: if you are already familiar with gdb, you can do this step simultaneously with Steps 4 and 5.

The Unix script command records your console session. Everything you type is recorded in a file called typescript. All of the output produced by programs that you run will also be captured.

You should double check that the typescript file actually recorded you compiling and using gdb. You can print out the contents of the typescript file using:

cat typescript


Step 7: save your answer

In Step 5, you remembered the last value of the variable pick before proj0.out crashed. Now, create and edit a text file called answer.txt. In this file, type in this last value of pick. Your file should have a single line that has one number.


What's in your submit directory

After you are done, you should have the following files in your proj0 directory.

Just leave these files in place and you are done.