Due: Thursday, February 17, 2022, 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 C language. Most macOS and Linux machines have a C compiler shipped with it. If you use a Windows machine, you probably need to install a C compiler. 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 don't want to bother with installation, you can use the machines in the robotics lab or Davis 101 and 102. The machines in the CS department have a C compiler installed already.
Please use the command line on a terminal to compile and run your programs.
You may use whatever editor you want. I recommend learning TextWrangler, XCode, or emacs, 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. Will it spill over into space that belongs to another fvariable? Don't forget to include string.h.
Define an int variable directly after the character array of fixed length. Give that int an initial value of 0. Then use strcpy to copy a too-long string into the char[] array. Then, have the program print out safe if the int variable has the initial value 0 and hacked if the int variable is non-zero. Depending on your compiler, the program should either crash or print "hacked".
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 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 wiki 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:
CS333_A_SP on the fileserver. The C code for each
task should be submitted
to the Project1 foler. 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.
tree command to generate this.
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 wiki report.
© 2021 Ying Li. Page last modified: .