Skip to content
Courses

C Memory Lab

Programming FoundationsLab 2 of 3 · Guided90 minutesLabBuilds on: Arrays, Strings, Pointers, and Memory

Download c-memory-v1.0.0.tar and save it in ~/underdunn-course/downloads. Expected SHA-256:

4b60bdc55093808418aeb46bb3d485ed19d454f2ee773ea5ff7ac9122f91e3e1
Terminal window
cd ~/underdunn-course/downloads
sha256sum c-memory-v1.0.0.tar
mkdir -p ~/underdunn-course/labs/c-memory
tar -xf c-memory-v1.0.0.tar -C ~/underdunn-course/labs/c-memory
cd ~/underdunn-course/labs/c-memory/c-memory-v1.0.0
find . -maxdepth 2 -type f -printf '%P\n' | sort

Stop right there unless the digest matches and memory-observer.c turns up in that listing.

Before you compile anything at all, make your predictions from the source alone. Open memory-observer.c, find the array declaration, and work out the following from its declarations:

  • the element count
  • the total array size in bytes
  • the size of a single element
  • the size of a pointer
  • the address difference you expect between adjacent elements
  • the first index that would be out of bounds

Then compile:

Terminal window
gcc -Wall -Wextra -Wpedantic -O0 -g -o my-observer memory-observer.c
./my-observer

Check the result with file my-observer. The absolute addresses will probably change between runs, and the size and distance relationships should stay explainable regardless.

Compare two adjacent element addresses and relate their difference to the element size. Then explain why the pointer size measures neither the array nor the object it points at.

Explain how many valid elements the array declares, why the pointer size differs from the array size, and why the first index past the last valid element is out of bounds. Derive that count from the declaration and from your own program output, and not from anything written on this page. Don’t test an out-of-bounds read, because undefined behavior is never required evidence.

Enter the element count and the pointer size in bytes from your original program output, using decimal values.

Check your answers

Enter the values you found. Your answers are checked in this browser without being sent anywhere.

Hint 1: strategyKeep array size, element size, and pointer size separate, because they answer three different questions.
Hint 2: evidenceFind the declaration, the sizeof expressions, and the loop condition, and use addresses only for valid indexes.
Hint 3: explicit commandRun the supplied compile command, compare the output against sizeof(array) / sizeof(array[0]), and use the loop’s strict bound.
  • A compiler warning appears: fix the first warning in your copy and rebuild it.
  • The address subtraction comes out differently: compare adjacent addresses from the same run, and convert the hexadecimal carefully.
  • The pointer size turns into an element count somewhere: label the pointer, the array, and the element as three distinct objects.
  • You’re tempted to read index five: don’t. The declaration and the loop bound already prove it invalid.

The build should be warning-free and the checker should accept your result. Adjacent element addresses should differ by the element size. Check that your last valid index stays inside the array; a one-past pointer is not another element.

Terminal window
cd ~/underdunn-course/labs/c-memory
rm -rf -- c-memory-v1.0.0
tar -xf ~/underdunn-course/downloads/c-memory-v1.0.0.tar -C .

For final cleanup, run only the first two lines, and keep the verified archive and any modified source you want.