Skip to content

Latest commit

 

History

History
25 lines (18 loc) · 749 Bytes

function-pointers.md

File metadata and controls

25 lines (18 loc) · 749 Bytes

Validity of function pointers

Disclaimer: This chapter is a work-in-progress. What's contained here represents the consensus from issue #72. The statements in here are not (yet) "guaranteed" not to change until an RFC ratifies them.

A function pointer is "valid" (in the sense that it can be produced without causing immediate UB) if and only if it is non-null.

That makes this code UB:

fn bad() {
    let x: fn() = unsafe { std::mem::transmute(0usize) }; // This is UB!
}

However, any integer value other than NULL is allowed for function pointers:

fn good() {
    let x: fn() = unsafe { std::mem::transmute(1usize) }; // This is not UB.
}