Due: Wednesday, September 20 2023, 11:59 pm
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.
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
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.
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.
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.
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.
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.
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).
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:
The following are some suggested extensions. You may also choose your own extensions of interest related to the project topic.
The submission of this project has three components:
tree command to generate this.
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: .