Terminology (5)Terms used in this lesson
Term on the front, definition on the back. Paste with tabs between fields and a new line between cards, or import the download in Anki as Basic.
- Panic
- A failure that starts native panic handling. Go runs deferred calls; Rust can unwind or abort according to its panic strategy.
- Undefined behaviour
- An operation the language places no requirements on; the whole run may then do anything.
- Defined but wrong
- An operation with a guaranteed result that is still not what you meant, such as a wrapped counter.
- Recover at the edge
- Catching a failure once per unit of work, such as a request, so the program can continue with the next.
- NDEBUG
- A macro that disables assert checks where <cassert> observes that definition.
What the language promises after a bad operation
A checked failure follows a specified route, such as an error value, exception or panic. A trap stops execution at a runtime or machine boundary. Undefined behavior removes the language’s requirements for the affected execution, so a result observed once cannot establish a reliable rule.
Check the particular operation and build assumptions. Bounds checks, assertions and arithmetic conversions have different contracts even within one language. A useful repair enforces the application’s rule before relying on a result or continuing with later requests.
01Past the end
Resolve a requested scene
A theatre control desk resolves script scenes and processes seat-booking requests. Its legacy event encoder stores a cumulative arrival count in one unsigned octet, so values above 255 cannot be encoded. Bad scene positions, failed bookings and an unrepresentable increment require different boundaries in the same request processor.
The play has four scenes. The program prints scene 4, then asks for scene 5, then prints curtain. Predict what happens.
scenes is a std::vector; the program reads scenes.at(4).
#include <iostream>
#include <string>
#include <vector>
int main() {
std::vector<std::string> scenes{"prologue", "duel", "storm", "finale"};
std::cout << "scene 4: " << scenes.at(3) << std::endl;
// scenes[4] would be undefined behaviour; at() checks the position instead.
std::cout << "scene 5: " << scenes.at(4) << std::endl;
std::cout << "curtain" << std::endl;
}
clang++ -std=c++2c -Wall -Wextra -pedantic main.cpp -o main && ./main
Use a fresh directory for each supplied main file. For TypeScript, set the package.json type field to commonjs while preserving its other fields, then use the guarded compile-and-run command below. Rust commands explicitly enable unwinding, overflow checks and debug assertions for these probes. The C++ read uses at(); unchecked access is discussed without executing it.
02What the build decides
Can the desk rely on this assertion?
A rule says seats must not be negative. The program breaks the rule with seats = -1 and checks it the usual way for each language. Predict what happens.
The rule is checked with assert, and NDEBUG is defined at the top.
#define NDEBUG
#include <cassert>
#include <iostream>
int main() {
int seats = -1;
assert(seats >= 0 && "seats must not be negative");
std::cout << "seats " << seats << '\n';
}
03Defined but wrong
Represent the next total
The hall counter holds one byte. It is at 250 and 10 people arrive. Predict the count.
The counter is a std::uint8_t; the sum is computed as an int and converted back.
#include <cstdint>
#include <iostream>
int main() {
std::uint8_t hall = 250;
std::uint8_t arriving = 10;
// The sum is computed as an int (260), then converted to uint8_t:
// unsigned conversion is defined to wrap.
hall = hall + arriving;
std::cout << "in hall " << int(hall) << '\n';
}
04Repair the theatre
Check scene numbers, keep booking, never wrap the hall count
Repair the control-desk request processor: report script positions outside the scene list, isolate each failed seat request, then encode arrival increments without wrapping the stored one-octet total.
The encoder accepts a group only when its intended cumulative total fits 0..255. Refusal leaves the prior total unchanged. This is an encoding requirement; the exercise does not calculate a venue admission policy.
Repair the theatre
Prevent a bad read, recover at the edge, then prevent a wrap.
Sources
Sign in to keep your read marks on every device. Sign in
Was this helpful?