CSE333 Homework 0

Out: Friday September 26, 2014
Due: Tuesday Sepember 30, 2014 by 11:59pm.

[ summary | part a | part b | part c | part d | how to submit | grading ]

Summary
For homework #0, you will fetch a source distribution that we have prepared. Next, you will trivially modify a simple hello world C application, use "make" to compile it, and run it to learn the magic code. You will also run a tool that checks your code for memory leaks. Finally, you will package up and submit your code.

As you can probably tell, the real goal of this homework is to make sure that all of the course infrastructure is working for you.

Part A -- Fetch the code
Overview

For this quarter, you'll fetch code from the course web site. The code is packages as a "gzip'ed tar archive" so you'll need to un-gzip and un-tar it to produce your working directory. Once you've done that, you'll have the homework files and can edit them locally until ready to turn them in. We'll use the course drop box for turn-in.

You may want to use some source control system to help manage your files, and to provide a crude form of backup for them. Popular options are git and subversion. For details on how to access a repository once you've set it up on your CSE account, see the Version Control Systems section at the bottom of the Computing Resources for Undergrads page.


Fetch the code

Create a directory to contain your cse333 projects. Click (or right-click if needed) on this hw0.tar.gz link to download the compressed archive containing the starter code and store it in that directory.


Expand the tar file

First, have a look at what is about to happen:

bash% tar tzf hw0.tar.gz
hw0/
hw0/hello_world.c
hw0/Makefile
bash%
The files listed will be created when we expand the tar file. The paths are relative to the current working directory, so you should see subdirectory hw0 created there.

bash% # expand the tar file
bash% tar xzf hw0.tar.gz
bash% cd hw0

Part B -- edit, compile, and run hello_world
In the hw0 directory, type make. This command will use the instructions in file Makefile to compile the hello_world application using the gcc compiler. Then run hello_world by typing the command ./hello_world. You should see one line of output from running the program:
bash% make
gcc -g -Wall -O0 -c -std=c99 hello_world.c
gcc -g -Wall -O0 -o hello_world hello_world.o
bash% ./hello_world
The magic code is: <xxxxx>
bash%

for some <xxxxx> we're not telling you here.

Now, edit hello_world.c using your favorite Unix-based editor (mine is emacs), and change the printf so that it instead says:

bash% ./hello_world
The magic word is: <xxxxx>
bash% 

Execute make to rebuild, and then re-run hello_world to make sure it does what you expect.

Part C -- experience the gdb debugger
You're unlikely to have any run time bugs in this homework, but let's use the debugger so that you know what it can do. Here is a session that launches the debugger, sets a breakpoint on source line 27 of hello_world.c, prints the values of a couple of variables and an expression, prints a "backtrace" of the stack (showing the sequence of procedure calls that led to executing line 27 of hello_world.c), and then continues execution. Type the italicized lines.
bash$ gdb ./hello_world 
GNU gdb (Ubuntu/Linaro 7.3-0ubuntu2) 7.3-2011.08
Copyright (C) 2011 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later 
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
For bug reporting instructions, please see:
...
Reading symbols from /home/zahorjan/cse333/hw0/hello_world...done.
(gdb) b 27
Breakpoint 1 at 0x400511: file hello_world.c, line 27.
(gdb) r
Starting program: /home/zahorjan/cse333/hw0/hello_world 

Breakpoint 1, main (argc=1, argv=0x7fffffffe0c8) at hello_world.c:27
27  printf("The magic code is: %X\n", a + b);
(gdb) p argv[0]
$1 = 0x7fffffffe394 "/home/zahorjan/cse333/hw0/hello_world"
(gdb) p a
$1 = -889262067
(gdb) p a+b
$3 = -559038737
(gdb) bt
#0  main (argc=1, argv=0x7fffffffe0c8) at hello_world.c:27
(gdb) c
Continuing.
The magic code is: <xxxxxxxx>
[Inferior 1 (process 6760) exited normally]
(gdb) q
bash$
Note: For gdb to be useful, you must give the -g switch to gcc when compiling. The make file we distributed does that.

Part D -- verify you have no leaks
Throughout the quarter, we'll also be testing whether your code has any memory leaks. We'll be using Valgrind to do this. Try out Valgrind for yourself, to make sure you can run it:
bash% valgrind --leak-check=full ./hello_world # there are two hypens before leak-check

Note that Valgrind prints out that no memory leaks were found.

Turn in
When you're ready to turn in your assignment, do the following:
  1. Create a README.TXT file in directory hw0 that contains your name, student number, and UW email address. As well, write a sentence that reveals the magic word you learned from Part B.

  2. Execute the following commands:
    bash$ ls
    hello_world  hello_world.c  hello_world.o  Makefile README.TXT
    bash$ make clean
    /bin/rm -f *.o *~ hello_world
    bash$ ls
    hello_world.c  Makefile README.TXT
    bash$ cd ..
    bash$ tar czf hw0_<username>.tar.gz hw0  # substitute your CSE user name
    bash$
    

  3. Verify that what you're turning in is what you expect to be turning in:
    $ tar tzf hw0_<username>.tar.gz 
    hw0/
    hw0/hello_world.c
    hw0/README.TXT
    hw0/Makefile
    
  4. Turn in file hw0_<username>.tar.gz using the course dropbox.

Grading
For hw0, we'll give you 1 point if you learn the magic code and correctly submit your code.