Exercise 2: Pointers & Arrays

Due:   Thursday, July 2 by 11:59 pm
Rating:   2 (note)
Learning Objectives:

Warm-Up Questions

This exercise includes a short, graded and required warm-up in questions.txt (in the starter). It has three steps:

  1. Predict: before you write or run any code, answer the two questions using only your understanding.
  2. Verify: once your DumpBytes works, add the suggested code to check your predictions, then remove that temporary code so your program still prints only the required output.
  3. Reflect: if a prediction was wrong, explain what you misunderstood and what you learned.

Do Step 1 first, before writing any code. Submit your completed questions.txt along with your code.

Problem Description

In this exercise you will write a small C program (ex2.c) that lets you see how data is actually stored in memory, one byte at a time. Additionally, you will write a function that sorts an array of bytes. The goal is to get comfortable with the close relationship between arrays, pointers, and the raw bytes.

You will not start from a blank file. The provided starter ex2.c (in your exercise repo) already contains:

  • the two function prototypes you must implement, and
  • a complete main that calls your functions.

Your job is to (1) write the bodies of the two functions described below, and (2) replace the three TODO_REPLACE_THIS placeholders in main with the correct length arguments. You should not need to change anything else in main.


Function 1: DumpBytes

void DumpBytes(void* data_ptr, int32_t data_len);

DumpBytes prints the raw bytes of any piece of data. Because the first parameter is a void*, the caller can hand it the address of an int, a float, an array, or anything else. DumpBytes simply treats that address as the start of a sequence of data_len individual bytes.

It should print one line containing, in this order:

  • the number of bytes, data_len, in decimal;
  • the starting address held in data_ptr, in hexadecimal; and
  • each of the data_len bytes, in lowercase hexadecimal.

The exact text, spacing, and formatting are important and are spelled out in the Output Format section below.


Function 2: CopyAndSort

void CopyAndSort(uint8_t src[], uint8_t dst[], int array_len);

CopyAndSort receives two arrays of bytes (a source array src and a destination array dst), along with array_len, the number of elements. You may assume both arrays have exactly array_len elements. The function does two things, in order:

  1. It calls DumpBytes on src, passing sizeof(src) as the length. Look closely at what this line prints when you run your program, and compare it against what you expected to see.
  2. It copies every element of src into dst, but rearranged into non-descending order. The original src array is left unchanged.

For example, if src holds {3, 2, 0, 8, 2}, then after the call dst should hold {0, 2, 2, 3, 8}.


Completing main

The provided main declares a few local variables and then calls your functions on them. In three of those calls the length argument is left as the placeholder TODO_REPLACE_THIS; you must replace each one with the correct value. Think carefully about what each call needs, since the right answer is not the same in every case.

Compilation

While testing, compile your program into an executable called ex2:

$ gcc -Wall -g -std=c17 -o ex2 ex2.c

Output Format

Every call to DumpBytes prints exactly one line in this format (your output must match exactly, including the capital The, spacing, and lowercase hex):

The 4 bytes starting at 0x7fff1081856c are: ff 01 30 4e
  • The length is printed in decimal; the address in hex; each byte as exactly two lowercase hex digits, separated by single spaces.
  • The printed address varies between runs (stack randomization) and is not checked for an exact value.
  • When data_len > 0, there must be no trailing space. When data_len == 0, a trailing space is fine.

Implementation Notes

This exercise builds on the close relationship between arrays and pointers discussed in lecture:

  • In an expression, an array name evaluates to a pointer to its first element.
  • Subscripting is pointer arithmetic: ar[i]*(ar + i).
  • An array parameter is really a pointer parameter, so the function receives a copy of a pointer, not the array. This has a concrete consequence you will rely on in CopyAndSort.

A few specific things to keep in mind as you implement:

  • You must cast the void* in DumpBytes before you can read individual bytes through it.
  • Use the hex format macros from <inttypes.h> (already included) to print a uint8_t and a pointer in lowercase hex, e.g.:
    uint8_t a_byte = 0xD1;
    printf("The byte is: %02" PRIx8 " -- enjoy!\n", a_byte);
  • Do not use a library sort (e.g., qsort); we recommend insertion sort. Remember to handle the degenerate zero-length array gracefully.

Style Focus

Names and Comments

Function names must match the spec exactly, including capitalization, since our autograder depends on it. Write a short comment above each function describing its behavior and purpose.


Format Specifiers and Constants

Use the correct format specifiers to avoid implicit casts and keep your code portable. Avoid magic numbers: prefer predefined constants (e.g., EXIT_SUCCESS), #defined names, and sizeof over unnamed literals.


Tools

Run the linter and valgrind periodically and resolve all issues before submitting:

$ ./cpplint.py --clint ex2.c
Done processing ex2.c
$ valgrind ./ex2
...
ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

Submission

Begin from the provided starter files (ex2.c and questions.txt) in your exercise repo. Submit ex2/ex2.c and ex2/questions.txt by creating an ex2-submit tag before the deadline.


Requirements for Full Credit

  • Compiles without errors or (non-exempt) warnings on CSE Linux machines with:
    $ gcc -Wall -g -std=c17 -o ex2 ex2.c
  • No runtime errors, memory leaks, or memory errors (valgrind clean).
  • A comment at the top of ex2.c with your name(s) and CSE/UW email(s).
  • Be pretty: layout, naming, and comments follow class style, and cpplint.py --clint runs clean.