Mettle

M0103Returning the address of a stack local

Reported by the compiler as an error or warning.

mettle explain M0103

A function returns a pointer into its own stack frame. The frame is reclaimed on return, so the caller receives a dangling pointer. This is a hard error.

Example

fn bad() -> *int64 {
    var local: int64 = 1;
    return &local;        // M0103
}

Fix: heap-allocate the value (new/malloc) or return it by value.

Related