Due: Monday, September 19, 2016, 11:59 pm

Part I: C Programming

For this part of the project, 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 TextWrangler, emacs, or XCode, although some folks like Eclipse.

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

        /**
         * A short description of the program
         *
         * Your Name
         * Date 
         */

        #include <stdio.h>
        #include <stdlib.h>
        
        /* A short description of the function, if necessary
         */ 
        int main (int arg, 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, float, double) and assigns each one a value (your choice). Declare an unsigned char * and then go through each data type and look how it is stored in memory. You may want to use the sizeof function to control the for loop. Calling sizeof(double), for example, returns the number of bytes used for a double.

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

              int i = 0x01234567;
              unsigned char *ptr;
    
              ptr = (unsigned char *)&(i);
    				

    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? How does this program tell you?

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

    Give the unsigned char * the address of itself. Then write a for loop that prints out and accesses memory in sequence using the unsigned char * from index zero onwards with no stopping condition (ok, start by printout out only the first 100 bytes or so). The only statement in the for loop should be a print statement showing the current index and the byte currently accessed by the unsigned 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 (or, if you are on a Mac, you can use the Activity Monitor), 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). Set up the structure so it should use an odd number of bytes.

    Write a program that allocates one of these structures. Then use the unsigned 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 (and don't forget to include string.h) 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. If the program crashes instead of printing hacked, that is a perfectly fine demonstration.

Part II: Selected Langauges

In this part of the project, you are expected to select four languages in addition to C. I recommend you to select the languages you are interested in and do not know yet. You will learn two of them in the rest of this course.

Tasks

Do some research regarding the four languages. For each of them, you will have a paragraph to describe it in terms of:

  1. The design purposes of the language
  2. When was the language developed
  3. It is a compiled language or interpreted language
  4. It is open source or prorietary

Extensions

  1. Write the shortest C program you can to generate a bus error. What is the difference with a seg fault?
  2. Find the floating point number in C to which you can add one and get back the same number.
  3. Make the safe/hacked C program use a string from the command line.
  4. What other kinds of run-time errors can you generate with very simple C programs?
  5. Do some research regarding a fifth language. Have a paragraph of description for it.

Submission

The submission of this project has three components:

  1. C codes: The C codes for each task should be submitted to the fileserver. Each task should have a C file, and the filename should specify the task name (e.g., task1.c). Note that the quality of your comments counts toward your grade. In addition to the top comments, necessary inline comments help readers to understand your codes.
  2. README: You need to submit a README file to the fileserver for your C codes of this project. The README file can be a .txt file. It should be well-organized in a way that readers can easily know the usage and the outputs of every C file. Your answers to the questions of every task should be included as well. In addition, the known bugs of each C file should also be indicated in the README file. Please also indicate the version of your C complier, your operationg system, and its version in the README file.
  3. Wiki write-up: The write-up of this project should have the following elements.
    • Title of the project and your name
    • A brief introduction to the languages you select
    • At least four paragraphs to describe the four selected languages

Please note that it is your responsibility to explicitly indicate the extensions you have taken. If you take the extensions in C language, please indicate explicitly in the codes and README file. If you take the extension in selected languages, please indicate explicitly in your wiki write-up.

© 2016 Ying Li. Page last modified: .