Classwork 2

This classwork will give you some good review (err... first encounter?) with some of the fundamentals of C programming, culminating in writing your own bash command from a C executable!


C-view


...like a review, but in C!

Recall that one of the distinguishing features in C compared to higher level languages like C++ and Java is that the programmer must manage their own requests for memory as well as when that memory is freed for reuse.

As such, there are a variety of memory-allocation request syntaxes used in C that expect us to request precisely how much memory (in bytes!) we need for a particular data structure.

Before continuing, refresh your memory (pun intended) on the different memory allocation syntaxes in C:
Good Wiki Summary

After reading the above, answer the following questions and then implement the functions that follow.


Problem 1: Report

For each of the following questions, provide a 1-2 sentence answer and save your responses in a plain text file report.txt.

  1. Different primitive types require different amounts of memory. Determine how many bytes are required for char, int, double types using the sizeof function. Show a simple C program that prints out these values.

  2. For each memory allocation method ((1) automatic / local, (2) static, (3) dynamic), describe:

    • The purpose / use of the allocation method in plain-English

    • Where the memory is reserved (what conceptual parts of RAM)

    • The associated memory allocation syntax in C

    • When the memory reserved in each case is freed

  3. Show how to reserve memory for an array of 5 ints using the (1) automatic notation, (2) malloc notation, and (3) calloc notation, then, provide the C code that will print out the addresses of each of the 5 ints (in any one case). What do you notice about the addresses in relation to what's being stored?

  4. Memory management is often difficult... so difficult in fact that we have names for common errors associated with memory mis-management. For each of the most common errors ((1) allocation failure, (2) memory leak, (3) double free):

    • Describe the error in plain-English.

    • Provide a C code snippet that demonstrates the error in practice.

  5. The following program commits a serious, but subtle, memory-management offense. What is it, and how would you repair it?

      #include <stdlib.h>
      #include <stdio.h>
      
      int* f() {
          int arr[5];
          for (int i = 0; i < 5; i++) {
              arr[i] = i;
          }
          return arr;
      }
      
      int main () {
          int* arr = f();
          for (int i = 0; i < 5; i++) {
              printf("%i\n", arr[i]);
          }
      }
    

Problem 2: C Warmup - Decoder

In C, ints and chars are easily cast to one another -- we simply use each character's ASCII encoding value as its int representation.

Design a C program decoder.c that takes in some arbitrary number of ints via the command line arguments, and then prints the characters, in sequence, that correspond to those ASCII character codes.

  Example Useage:
  > ./a.out 72 73 89 65
  HIYA
  > 

Notes:

  • You may include any C library that you wish to accomplish this task -- Google will be your friend!

  • Any allocated memory must not waste any space (i.e., you should never reserve more memory than is needed for the task).

  • If your program has any memory leaks, your score will leak as well.


Problem 3: C Warmup - Ole Yeller

Write a program ole_yeller.c that accomplishes the following:

  1. Write a C function char* yell (char* whisper); that takes the given C-string whisper and returns a new C-string with every letter capitalized, and with an exclamation mark appended to the end.

  2. Using your yell method, design a main method that prints the yell-ified version of each of a user's entered command line arguments to the program.

  Example Useage:
  > ./a.out this is a tame sentence
  THIS! IS! A! TAME! SENTENCE!
  >

Notes:

  • You may include any C library that you wish to accomplish this task.

  • Any allocated memory must not waste any space (i.e., you should never reserve more memory than is needed for the task).

  • If your program has any memory leaks, your score will leak as well.


Re-bash


...like a rehash, but implementing a bash command using system calls!


Problem 4: My Little Linker

Create a C program l2d.c (link to desktop) that takes a single command line argument: an absolute path to a file, and then creates a symbolic link to that file on the user's desktop.

Notes:

  • You will need to look up and discover how to use the proper system call to accomplish the above.

  • Apropos, you may *ONLY* use system calls to accomplish the above -- no relying on standard libraries or external functions! (you're of course fine making your own variables and writing your own functions if it helps).

  • You will receive full credit if the link created on the desktop is simply named newlink, but will receive an extra bonus if the link's name is link2<linkedFileName> (where <linkedFileName> is, of course, a placeholder for the name of the file being linked).

Once you have a working executable, look up how to name it using a flag to gcc, store it somewhere you won't lose it, and then create a bash alias to it so that you can use l2d anytime, anywhere!



Submission

What

Place all requested *source* files (no build products) into a zip archive before submitting. (report.txt, decoder.c, ole_yeller.c, l2d.c)

Ensure that your name, and the names of all group members (up to 4), appear at the top of your source files.


How

We will (unfortunately) be using Brightspace to submit this assignment, though you should always be using version control to organize your assignments!

To submit your assignment:

  1. Find this classwork listing on Brightspace.

  2. Upload the .zip file containing your submission.

  3. Make sure to click "submit" after the upload is complete!



  PDF / Print