A heap escape occurs when a variable must remain valid after its function returns. If the compiler cannot prove that no later reference exists, it allocates the variable on the shared heap instead of the function's stack. section 12.5.1
Returning a local pointer
Returning &z from sumPtr makes stack allocation unsafe: z would disappear with sumPtr's stack frame, leaving c pointing at an invalid address. Heap allocation preserves z for *c, but adds allocation and garbage-collection cost.
Heap escapes matter because they can change both lifetime and performance:
- Stack allocation is faster and self-cleaning; heap allocation requires garbage collection and can increase latency. section 12.5.1
