CS 333: Assignment #1

C Programming

For this assignment, you will write a few simple C programs to experiment with data types, memory access, arrays, and memory management. The intention is to break stuff, so have fun.


Tasks

Please use the command line on a terminal/xterm to compile and run your programs. You can use the Terminal program, or start up X-windows by typing xterm in a Terminal window.

You may use whatever editor you want. I recommend learning emacs or XCode, although some folks like Eclipse. TextWrangler is also fine.

For all of these C programs, your code should have the following form.

/*
A comment with your name and a short description of the program
*/

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[]) {

  // your stuff here

  return(0);
}

To compile a C program, use gcc. The -o option lets you specify the name of the executable. To run your executable, type it's name on the command line. You may need to put a ./ in front of the name to tell the shell to look in the current directory for the executable.

$  gcc -o myexec  mycfile.c
$  ./myexec

  1. Memory access: write a program that declares a variable of each of the basic types (char, short, int, long, double) and assigns each one a value (your choice). Declare a char * and then go through each data type and look how it is stored in memory.

    You can do this by first taking the address of each variable, casting it to a char *, and assigning it to your char * variable. For example:

    
    int ai = 0x01234567;
    char *ptr;
    
    ptr = &(ai);
    
    
    Then you can loop through the char pointer, treating it as an array. The function sizeof returns the number of bytes in a data type.

    Note how the data is stored in memory. Is the machine you are using a big-endian or little-endian machine?

  2. Make a program that declares and assigns some variables. Have the last variable you declare be a char *.

    Give the char * the address of itself. Then write a for loop that prints out and accesses memory in sequence using the char * from index zero onwards with no stopping condition. The only statement in the for loop should be a print statement showing the current index and the by byte currently accessed by the char *. Be sure to put a newline at the end of the print format string so the information is flushed to the screen (try it without a newline and see the difference).

    Run the program and see how much memory you can access. What happens at the end of the process? Can you find the other variables?

  3. Write a program that repeatedly allocates a small amount of memory in a loop that goes on for a long time. Just use a single variable to point to the memory (this is very bad, but it makes a good example). Using the terminal command top, watch the memory requirements for your program grow.

    Now add a free statement to the loop that frees the memory you just created. Then watch your memory requirements using top

  4. Using the typedef statement, make a struct that has 3-4 different variables (including some char and short types. Write a program that allocates one of these structures. Then use the char * trick to look at how the memory is laid out. Does the sizeof result match your expectation? Are there any gaps in the way the fields of the structure are laid out?
  5. Demonstrate how, by passing a string that is slightly too long to a function, you can overwrite a decision variable within the function if the function uses strcpy to copy the parameter to a local character array of fixed length. Make it a clear demonstration by having the function print out safe if the decision variable has the value 0 and hacked if the decision variable is non-zero.

Extensions


Writeup

The writeup for each project should be a brief summary of what you did along with some code examples, terminal output, or screen shots, depending upon the assignment. Please organize the writeup as follows.

  1. Title of the project and your name
  2. An abstract describing what you did in 200 words or less.
  3. A brief description of code you wrote.
  4. Results.
  5. A brief description of what you learned.

Handin

Make your writeup for the project a wiki page in your personal space. If you have questions about making a wiki page, stop by my office or ask in class.

Once you have written up your assignment, give the page the label:

cs333f10project1

You can give any page a label when you're editing it using the label field at the bottom of the page.

Do not put code on your writeup page or anywhere it can be publicly accessed. To hand in code, mount the Academics volume on fileserver1 and put your code in the private subdirectory under your user name. If your directory is not there, email the code to the prof.