Due: Wednesday, September 20 2023, 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.

You need to install a C compiler to program in the C language. Most macOS and Linux machines have a C compiler shipped with it. A way to check whether your machine has a C compiler is to open a terminal and type gcc --version. If you don't get any message indicating that the command gcc doesn't exist, your machine should have the compiler already. You probably need to install a C compiler if you use a Windows machine. If so, you can start with Cygwin, a Linux emulator for Windows which gives you a flavor of Linux and enable you to install a C compiler easily. If you have Visual Studio, you can install the components you need for building C apps. If you don't want to bother with installation, you can use the machines in the robotics lab (Davis 122), Davis 101, and Davis 102. The machines in the CS department have a C compiler installed already. You can remotely access the machines in the robotics lab. E.g., if you'd like to access fili (the name of a computer in Davis 122), you can use ssh yourColbyAccount@fili.cs.colby.edu.

Tasks

Please use the command line on a terminal to compile and run your programs.

You may use whatever text editor you want. I recommend learning TextWrangler or XCode, although some folks like Eclipse and Visual Studio.

Implement each task in an individual C file. 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 its 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. Continuing the example above, you can use printf with the format string "%02X" to print out each byte of the int.

    	for(i=0; i<sizeof(int); i++) {
    	    printf("%d: %02X\n", i, ptr[i]);
    	}
    

    Note how the data is stored in memory and answer the following questions in your README.txt.

    1. Is the machine you are using a big-endian or little-endian machine?
    2. How does the program output tell you?
    Look up the definition of big-endian and little-endian to support your answer.

  2. Make another 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 printing 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 appropriately (try it without a newline and see the difference).

    Run the program and see how much memory you can access, and answer the following questions in your README.txt.

    1. What happens at the end of the process?
    2. Can you find the variables defined in your C program?

  3. Write a new 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.

    In your README.txt, briefly describe the memory requirements when using and not using the free statement.

  4. Make a struct that has 3-4 different variables (including some char and short types). Set up the struct so it should use an odd number of bytes.

    Write a program that allocates memory for the struct. Then use the unsigned char * to look at how the memory is laid out. Answer the following questions on your README.txt.

    1. Does the sizeof result match your expectation?
    2. Are there any gaps in the way the fields of the structure are laid out?

  5. Demonstrate whether the strcpy function is robust in your C compiler by passing a string that is slightly too long to a local character array of fixed length. Don't forget to include string.h.

    You first define a decision variable after the character array of fixed length before using strcpy. Then, make it a clear demonstration by having the program print out safe if the decision variable has the initial 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.

    Briefly describe your observation in your README.txt.

Part II: Selected Langauges

In this part of the project, you are expected to select three languages other than C that you do not know. Look at the Programming Languages section in the Course Outline page and pick one from each list (A, B, and C).

Tasks

Do some research regarding the three languages. For each of them, you will have a paragraph on your Google Sites report to describe it in terms of:

  1. the design purposes of the language,
  2. the main uses for the language,
  3. when the language was developed,
  4. whether it is a compiled or interpreted language, and
  5. whether it is open source or prorietary.

Extensions

The following are some suggested extensions. You may also choose your own extensions of interest related to the project topic.

  1. Write the shortest C program you can to generate a bus error. What is the difference from 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 in Part I 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 fourth language. Have a paragraph of description for it on your written report (on Google Sites).

Submission

The submission of this project has three components:

  1. C code: Create a folder for Project1_<username> (e,g. for Stephanie, this would be Project1_srtaylor) and place all of your C code into it. 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-level comments, block-level comments help readers to understand your code.
  2. README: For each project you must submit a README file along with your C code. The README file can be a .txt file. It should contain the following information.
    • Project identifier, your name, and the submission date.
    • The URL of your Google Sites report.
    • The directory layout for your handin folder. You can use the Unix tree command to generate this.
    • Your operating system and C compiler (e.g. use gcc --version).
    • For each task, include how to compile your program, how to run your program, known bugs if there is any, and expected output of the program. You should include the actual output of running the program if the output is no more than 100 liens. Otherwise, give a brief version of the output. This should include both your C programs and programs written in other languages.
    • Your answers to all the questions in the C tasks.
    You can use this README template.
  3. Google Sites Report: The report of this project should have the following elements.
    • A home page for your CS333 projects. The home page should have a title (e.g., Ying's CS333).
    • A subpage under the home page that should include:
      • Title of the project (e.g., Project 1)
      • A brief introduction to each languages you selected
      • One section per language that contains a logo of the language and a brief introduction answering the five questions listed in Part II.

      This sample home page and project 1 page are for your reference. You don't need to worry about the JS and R sections on the home page. They are for the following projects. Please log in to Google Sites using your Colby account before clicking the links.

      Here is an instruction for you to quickly start using Google Sites for CS333. Please log in to Google Sites using your Colby account before clicking the links. You can also find other resources on the Internet if you want to know more about Google Sites.

To submit your code and README, zip your Project1 folder (note that the zip file should have the name Project1_<username>.zip) and upload it to the Google Classroom assignment for Project 1 (NOTE - Stephanie will create the classroom assignment and link to it here.)

Please note that it is your responsibility to explicitly indicate the extensions you have completed. If you take any of the C extensions, please indicate them in your code and README file. If you undertake extensions in one of your selected languages, please indicate them in your Google Sites report.

© 2023 Stephanie Taylor. Page last modified: .