Pointer Lab: Looking at Pointers!

Released: 12:00 AM Tuesday, January 28th, 2020.

Due: 11:59 PM Friday, February 7th, 2020.

Here, we will investigate the nature of pointers. They aren't here to hurt us. They are here to help us!

Weight: 5% of your total 50% Lab Grade

Introduction

This lab will give you practice with pointers and pointer arithmetic. Pointers are a critical part of the C language. C gets out of your way and lets you mess with memory rather freely. Therefore, learning to use pointers and their nuances will aid you in exploring the memory layout of a system and how memory allocation works in the future.

The assignment contains a skeleton for some brand new programming puzzles! Each puzzle is a relatively empty function that has a comment block preceding it describing exactly what the function must do and what restrictions exist on your potential implementation.

Your task, should you choose to accept it (please do) is to complete each function.

Rules

Generally, each puzzle has some generic rules which can be relaxed if mentioned by the comment block. However, the following rules will apply to all:

You will start working with basic pointers and use them to compute the size of different data items in memory, modify the contents of an array, and complete a couple of pointer “puzzles” that deal with alignment and array addressing.

Downloading

Your lab materials are contained in an archive file called pointerlab.zip, which you can download to your Linux machine as follows:

wget https://wilkie.github.io/cs449/labs/02/pointerlab.zip

This will download the pointerlab.zip file to your (private) directory on the thoth Linux machine (make sure you are logged into this!) in which you will do your work. Then issue the command:

unzip pointerlab.zip

This will cause a number of files to be unpacked in the directory pointerlab-handout. Navigate to that directory and look at the files. The only file you will be modifying is pointer.c. The pointer.c file contains a skeleton for each of the programming puzzles.

What are these??

So, you will be updating the pointer.c file to perform the following tasks:

ptr1: 0x0
ptr2: 0x3F
return: 1
ptr1: 0x0
ptr2: 0x40
return: 0
ptr1: 0x3F
ptr2: 0x40
return: 0
ptr1: 0x3CE
ptr2: 0x3EF
return: 1
ptr1: 0x3CE
ptr2: 0x404
return: 0

Essentially, you are writing the proper C code for the following pseudo-code (given an array arr and length n):

for i = 0 to n - 1
  minIndex = i
  for j = i + 1 to n
    if arr[j] < arr[minIndex] 
      minIndex = j
    end if
  end for

  swap(arr[i], arr[minIndex])
end for

You definitely are allowed to use loops, ifs, and such on this one. And, hint hint, I think swapInts will be very useful, once you have implemented it.

Testing

We have included the following tools to help you check the correctness of your work automatically:

thoth $ make
thoth $ ./ptest
thoth $ python dlc.py

This will run silently unless it detects a problem, such as an illegal operator, constants usage, or non-straightline code (loops, ifs) in the pointer.c code. Although, I believe it will print out an empty list such as [] if it encountered no problem.

Just like in the prior lab, the dlc.py script enforces a slightly stricter form of C than the normal compiler, gcc. In particular, in a block (enclosed in curly braces) all your variable declarations must appear before any other type of statement (basically a C89 pedantic style). For example, it will complain about the following:

int foo(int x) {
  int a = x;
  a *= 3;
  int b = a; /* ERROR: Declaration not allowed here. */
}

Instead you must declare your variables first, like so:

int foo(int x) {
  int a = x;
  int b;
  a *= 3;
  b = a;
}

The dlc.py script also does not like the non-standard binary constant notation such as 0b10011010 so you will have to avoid this and use hex instead.

Submission

You should hand in your pointer.c solution file using Gradescope. It may be a couple of days until the assignment is in Gradescope, so if you finish very quickly, just wait a couple of days and check again. If you’re using a Shell Terminal, you can use scp to copy your file from the remote machine to your local machine as follows:

scp username@thoth.cs.pitt.edu:/path/to/remote/file /path/to/local/file

If you’re using Windows, you can download the WinSCP software and enter with host thoth.cs.pitt.edu and then your username and password. Once in, locate your pointer.c file and simply drag and drop to your Windows computer file system. There are also free GUI clients for Linux or Mac such as FileZilla (https://filezilla-project.org/).