Skip to main content

Practice ยท 1 of 5

Read Inside the Lifetime

CHALLENGE
Difficulty: advanced+25 XP

The helper below is already in your editor โ€” and it is deliberately buggy: ``c static int g_probe = 7; static int *leak_local(void) { int local = g_probe; return &local; } ` The pointer it returns is *indeterminate* the moment local dies: dereferencing it is UB, and even *comparing* it (say, leak_local() != 0) is UB โ€” the optimizer exploits that and folds the check away. Never touch a dangling value. Implement int safe_read(void): the defined counterpart โ€” take the address of an automatic int initialized from g_probe`, dereference the pointer *while the object is alive*, and return the value it reads.

Back to lesson: Practice: Sequencing and Lifetime Drills