⚠️ This is not the current iteration of the course! Head here for the current offering.

CSCI 1310 Homework 1: Computer Systems Basics

These homework exercises are for graduate students taking CSCI 1310. You will hand in your answers via GitHub and the grading server.

Please use this link to accept the assignment and create a repository for your homeworks. Inside the repo, you find a file called hw1.md. Please write your answers into the sections for each question in this file.

1. C Programming and Memory Layout

QUESTION 1A. Consider the code below, and write an implementation of the set_pointer() function.

int main(int argc, char** argv) {
  if (argc >= 3) {
    char* the_first_argument;
    char* the_second_argument;

    // set `the_first_argument` to point to the same string in memory as the first
    // argument to the program, and `the_second_argument` to point to the second
    // argument. You'll need to choose the arguments and return type
    // of `set_pointer`.
    set_pointer(YOUR_ARGS);
  }
}

QUESTION 1B. Using the provided figure (a vertical version of the memory picture we've drawn in lectures), and assuming standard address ranges for the memory segments (see lecture notes), draw the memory structure that will be in place at the end of the following program:

#include <stdio.h>
#include <stdlib.h>

long a = 0x1234;
const int b = 1234;

void f(int l) {
  unsigned short x = 99999;

  char* s = (char*)malloc(l);
  sprintf(s, "Yay CS0300!");

  a = &x;
  *(s + 6) = '1';
  *(s + 8) = '1';
}

int main() {
  int l = 12;
  f(l);
}
Address        | Value stored (hex bytes)   |
---------------+----------------------------+
               |                            |
---------------+----------------------------+
               |                            |
---------------+----------------------------+
               |                            |
---------------+----------------------------+
               |                            |
---------------+----------------------------+
               |                            |
---------------+----------------------------+
               |                            |
---------------+----------------------------+
 ...           | ...                        |
---------------+----------------------------+

Add as many rows as you need. You may write
multiple bytes' hex values into the second
column, but please put a consistent number
of bytes.

Hint: it may help to run the program augmented with appropriate extensions.

QUESTION 1C. Give three different ways to allocate an array of integers in C. Your examples should use different syntax and locate the array in different memory segments.

QUESTION 1D. For each of the following real programs and data structures, what is the appropriate memory lifetime (and thus, segment) to use? Briefly justify your choice.

  1. The format string passed to printf (e.g., "%s\n").
  2. The char array that your shell reads commands into. [Hint: shells have an upper bound on the command length; on my computer, it is 2097152 characters (you can check this via getconf ARG_MAX).]
  3. The memory your browser uses to display the course logo:
  4. The memory holding the contents of the webpage.

QUESTION 1E. Create a structure definition that contains exactly 19 bytes of data and 5 bytes of padding (for a total size of 24 bytes).

2. Undefined Behavior

QUESTION 2A. Give two examples of when undefined behavior occurs in the C language. Illustrate each example with a code snippet of your own (i.e., not from the lecture demos).

QUESTION 2B. Bruno Biteater argues that undefined behavior isn't a problem as long as the program happens to work correctly when compiled and run. Why is Bruno wrong? What could go wrong?

Handin

When you're done, commit your solutions, push them to GitHub and head to the Grading Server to register your repository.