C Memory Lab
Prerequisites and supported environment
Section titled “Prerequisites and supported environment”Download c-memory-v1.0.0.tar and save it in ~/underdunn-course/downloads. Expected SHA-256:
4b60bdc55093808418aeb46bb3d485ed19d454f2ee773ea5ff7ac9122f91e3e1Milestone 1: verify and extract
Section titled “Milestone 1: verify and extract”cd ~/underdunn-course/downloadssha256sum c-memory-v1.0.0.tarmkdir -p ~/underdunn-course/labs/c-memorytar -xf c-memory-v1.0.0.tar -C ~/underdunn-course/labs/c-memorycd ~/underdunn-course/labs/c-memory/c-memory-v1.0.0find . -maxdepth 2 -type f -printf '%P\n' | sortStop right there unless the digest matches and memory-observer.c turns up in that listing.
Milestone 2: predict from source
Section titled “Milestone 2: predict from source”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:
gcc -Wall -Wextra -Wpedantic -O0 -g -o my-observer memory-observer.c./my-observerCheck the result with file my-observer. The absolute addresses will probably change between runs, and the size and distance relationships should stay explainable regardless.
Milestone 3: connect values to addresses
Section titled “Milestone 3: connect values to addresses”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.
Milestone 4: defend the boundary
Section titled “Milestone 4: defend the boundary”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.
Layered hints
Section titled “Layered hints”Hint 1: strategy
Keep array size, element size, and pointer size separate, because they answer three different questions.Hint 2: evidence
Find the declaration, thesizeof expressions, and the loop condition, and use addresses only for valid indexes.Hint 3: explicit command
Run the supplied compile command, compare the output againstsizeof(array) / sizeof(array[0]), and use the loop’s strict bound.Common failures
Section titled “Common failures”- 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.
Check your result
Section titled “Check your result”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.
Reset and cleanup
Section titled “Reset and cleanup”cd ~/underdunn-course/labs/c-memoryrm -rf -- c-memory-v1.0.0tar -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.