Lab 0: C Warmup
|Assigned||Monday, March 28, 2016|
|Due Date||Monday, April 4, 2016 at 5:00pm|
|Files||See "Acquiring the code" below. (Direct link here: arrays.c)|
|Submissions||Submit your completed
The purpose of this assignment is to gain some very basic experience with C programming, including such topics as arrays, stack allocation, heap allocation, function calls, and memory safety (or the lack thereof). It is a credit/no credit exercise, and should take a short time to finish. Instructions on what to do are in the comments of the code you will download below.
Setting up your working environment
To do the programming assignments in this course, you will either need 1) a virtual machine that you can run on your own laptop or desktop (if it supports 64-bit x86) or 2) access to the CSE instructional cluster (named "attu") or 3) the instructional Linux machines in the lab. Only CSE majors will be able to run on attu or on the instructional Linux machines in the lab as they require a CSENetID.
All of you should be able to use a virtual machine. To install a virtual machine player on your own computer, follow the CSE Home VM instructions. If you have problems, you are probably not alone, so check out the course Discussion Board for any relevant discussions (or start a discussion yourself). You need to download the rather large VM file itself (it is over 3GB, so do this when you have good high bandwidth connectivity). This is a full linux environment with all the software we'll need this quarter pre-installed.
Acquiring the code for this assignment
You can download arrays.c here
from a browser. Alternatively, use
wget to download it
from the command line. In a terminal, run:
This should download arrays.c, storing it in your current working directory. A lecture on some basic Unix commands is available here if you need help with this step.
Editing, compiling, and running the code
Now that you have acquired the source file, open arrays.c in your favorite text editor. The arrays.c file contains a number of TODOs, which you are expected to complete. Most have a proceeding line that says "Answer:", which is where you should leave an answer to the question(s) posed in the TODO. One TODO requires you to write some code, which you should place immediately after the comment block that describes what to do.
The source file arrays.c won't do you any good by itself; you need a compiler (specifically the GNU C compiler) to compile it to an executable format. The GNU C compiler is available on the CSE home VM, attu, the instructional Linux machines in the lab, and most popular variants of Linux, such as Ubuntu and Fedora. You are free to use whichever machine you like, although we will only provide support for the CSE home VM, attu, and the instructional Linux machines in the lab.
Using any one of these machines, open a terminal and execute
gcc -v. On my machine, here is what I see:
$ gcc -v Using built-in specs. COLLECT_GCC=/usr/bin/gcc COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/5.1.1/lto-wrapper Target: x86_64-redhat-linux Configured with: ../configure --enable-bootstrap --enable-languages=c, c++,objc,obj-c++,fortran,ada,go,lto --prefix=/usr --mandir=/usr/share/ man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com /bugzilla --enable-shared --enable-threads=posix --enable-checking=rel ease --enable-multilib --with-system-zlib --enable-__cxa_atexit --disa ble-libunwind-exceptions --enable-gnu-unique-object --enable-linker-bu ild-id --with-linker-hash-style=gnu --enable-plugin --enable-initfini- array --disable-libgcj --with-default-libstdcxx-abi=c++98 --with-isl - -enable-libmpx --enable-gnu-indirect-function --with-tune=generic --wi th-arch_32=i686 --build=x86_64-redhat-linux Thread model: posix gcc version 5.1.1 20150618 (Red Hat 5.1.1-4) (GCC)
The output tells me a bunch of the configuration options for the my installation of GCC as well as the version number, which is 5.1.1. Assuming that you have saved the arrays.c source file somewhere on your machine, navigate to that directory, and then use GCC to compile it with the following command:
gcc -g -Wall -std=gnu99 -o arrays arrays.c
It's not that important right now for you to know what all of these
options do, but
-g tells the compiler to include debugging
-Wall says to print warnings for all types of
-std=gnu99 says to use the C99 standard
(now only 15 years old!),
-o arrays instructs the compiler
to output the executable code to a file called arrays, and
arrays.c is the source file being compiled.
Having executed that command, you should be able to see a file named arrays in the same directory:
$ ls arrays arrays.c
The arrays file is an executable file, which you can run using
./arrays. On my machine, here is what I see:
$ ./arrays Filling an array at address 0x7fff6b4e3e60 with 10 values Done! Filling an array at address 0x7fff6b4e3eac with 1 values Done! Filling an array at address 0x7fff6b4e3e90 with 4 values Done! Filling an array at address 0x11ca010 with 5 values Done!
If you look through the source code of the arrays.c file, you should be able to match up each line that is printed with the output that you see in the console.
With that, you should have everything you need to complete the
assignment. Note that every time you want to test a modification to
the source file, you will need to use the
gcc -g -Wall -std=gnu99 -o arrays arrays.c command to
produce an updated arrays executable file.
Checking your work
When you have finished the exercise, please take the time to ensure that your code both compiles without warnings (the GCC command will output lines that say "warning:" if you have warnings) and is readable. In particular, please try to make the format of your code match that of what was given to you.
Submitting the exercise
Once you have completed the exercise, submit your arrays.c source file using the Assignment Drop Box for the course.