Hands-on Practice
Estimate Time: 7 min read
A program can store text in a character array, where each position holds one character. When a program reads text, the amount of input and the space reserved for it both matter: the program needs to handle the text without disrupting other work.
At the neighborhood co-op’s shipping desk, Jo maintains a small kiosk that prints parcel routing labels. The connected label printer accepts a single routing code of up to 15 characters. Jo wants the kiosk to print a label for a valid code and leave the printer untouched when a code is too long. To match the printer’s field, Jo stores each code in a 16-character array: 15 positions for the code and one for its ending marker. Jo chose a simple input operation so staff could type a code and get a label quickly.
The kiosk should follow the printer’s limit regardless of what a staff member types. In this lab, you will help Jo test the kiosk’s normal behavior, investigate what happens with input beyond the printer’s limit, and improve the program so its behavior matches the shipping desk’s requirement.
Learning Objectives
- Use character arrays and strings to reason about how a program stores and processes text.
- Trace how input moves through a program and affects its output.
- Use program tests and a memory-safety tool to investigate whether the kiosk preserves its input requirement.
- Improve the program so it handles routing codes according to the printer’s limit.
Task 1: Establish the Kiosk’s Normal Behavior
Before Jo changes the kiosk, the shipping team wants to confirm what it does with routing codes that fit the printer’s field. The kiosk’s complete source is shown below:
#include <cstdio>
int main() {
// Match the printer field: 15 code characters plus the string terminator.
char route_code[16];
std::printf("Routing code: ");
// Read the code entered at the shipping desk.
if (std::scanf("%s", route_code) != 1) {
std::printf("No routing code received.\n");
return 1;
}
// Send the entered code to the label printer.
std::printf("LABEL: %s\n", route_code);
return 0;
}
Copy this complete source into a new file named label_kiosk.cpp in your working
directory. Keep that file for the later tasks.
The array reserves 16 character positions. The input operation reads one whitespace-delimited code, and the final output represents the label sent to the printer. If no code is entered, the program prints a message and stops. Jo chose the array size to match the printer’s 15-character field, with one additional position for the string terminator.
These commands assume Linux or a compatible POSIX shell, a C++17-capable g++ with
AddressSanitizer support, and Python 3 for the long-input test in Task 2. Work in a
directory where you can create and compile files; no services or elevated permissions
are required.
Compile and run the program from the directory containing label_kiosk.cpp:
g++ -std=c++17 -Wall -Wextra -g -O0 -fsanitize=address -fno-omit-frame-pointer label_kiosk.cpp -o label_kiosk
./label_kiosk
The program waits for a routing code.
Try a short code such as NORTH-12, then run it again with a 15-character code such as
NORTH-DOCK-0042. The sanitizer options enable a tool that can report certain memory
errors during execution; inputs that fit should still let you observe the kiosk’s
ordinary output.
Your Task. Record the kiosk’s output for both routing codes and explain what these runs establish about its normal behavior for codes within the printer’s limit.
Task 2: Investigate an Overlong Routing Code
The normal tests show the behavior Jo expects when the entered code fits the printer
field.
However, staff sometimes paste codes from another system, and Jo wants to know how
the kiosk behaves when a pasted value is longer than expected.
The same input and storage code from label_kiosk.cpp is relevant: the array has a
fixed capacity, while the input is typed at run time.
Use a long, harmless test value to investigate the behavior.
This command supplies 80 A characters to the sanitizer-enabled kiosk without requiring
you to type them all:
The sanitizer may print a diagnostic and stop the program. Look for the diagnostic’s error category and the source location it identifies. The program’s behavior without a sanitizer is not guaranteed when it writes outside the space reserved for an array; possible outcomes can vary between runs and builds. The diagnostic is evidence about the instrumented run, not a prediction that every ordinary run will fail in the same way.
Your Task. Produce an execution with the long test value that gives observable evidence of what happens, then explain how that result relates to Jo’s 15-character printer requirement and the array used by the kiosk.
Task 3: Make the Kiosk Enforce the Printer Limit
Jo’s investigation has established that the kiosk must handle pasted input as well as
carefully typed codes.
The shipping desk’s requirement remains the same: print a routing code only when it
contains no more than 15 characters, and do not print a shortened label for a longer
code. Jo asks you to update label_kiosk.cpp while keeping the kiosk’s simple one-code
input and label output.
The important behavior is concentrated in the code that declares route_code, reads the
input, and prints the label.
Your revised program should distinguish an overlong code from a valid one before sending
anything to the printer.
You may choose a different way to store the input, but test the boundary carefully: a
code of exactly 15 characters must remain valid, while a code longer than 15 must be
rejected rather than printed as a label.
Recompile the revised program with the sanitizer options from Task 1. Test a short code, a 15-character code, and the 80-character value from Task 2. Check both the output and whether the sanitizer reports a memory error. Keep the tests local to this kiosk program.
Your Task. Modify label_kiosk.cpp so the kiosk prints valid codes of up to 15
characters and rejects longer input without printing a label.
Provide the three test results as evidence, and explain how the revised behavior
preserves the shipping desk’s printer requirement.