Challenge 3: What a Dangling Pointer Is, and Why C Allows It While Rust Refuses To Compile It — Possible Solution ==================================================================== A DANGLING POINTER is a pointer whose value (a memory address) still looks like a perfectly ordinary, valid address, but the memory it actually points to is no longer valid -- it's been freed, or it belonged to a local variable whose function has already returned. The pointer itself doesn't change or become obviously "broken" when this happens; it silently continues holding the same address, even though using that address to read or write data is no longer safe. Why C allows creating and using one: per the chapter, C has no runtime or compile-time mechanism that tracks how long a piece of memory remains valid relative to how long a pointer to it is kept around. The language simply trusts the programmer -- nothing checks whether the memory a pointer refers to is still "alive" at the moment it's dereferenced. Doing so is undefined behavior: it might crash immediately, might silently return garbage, or might even appear to "work" by coincidence, since the memory hasn't been overwritten by anything else yet -- none of which is guaranteed, which is what makes it a genuinely dangerous class of bug rather than a predictable one. Why Rust's borrow checker refuses to compile a program that could produce one: Rust tracks, at compile time, how long every piece of data lives (its "lifetime") and how long every reference to that data is allowed to be used. If the compiler can determine that a reference could possibly still be in use after the data it points to has gone out of scope or been freed, it rejects the program outright, before it ever runs -- turning what would be a silent, unpredictable runtime bug in C into an explicit, impossible-to-ignore compile-time error in Rust. This is exactly the mechanism the chapter identifies as Rust's own founding goal, first named all the way back in `rust1-1`: memory safety enforced by the compiler, not left to the programmer's own discipline. WHY THIS WORKS AS AN ANSWER ------------------------------ This defines a dangling pointer precisely (a valid-looking address pointing to invalid memory), explains why C has no mechanism to catch it (no lifetime tracking at all), and explains what Rust's borrow checker specifically DOES track (lifetimes, at compile time) that lets it catch the exact same class of bug before the program can even be built -- tying the answer back to the course's own opening framing rather than treating the two languages' behavior as an arbitrary difference.