What is wrong with the function int& getValue() { int local = 5; return local; }?
Correct answer A. It returns a reference to a local variable that is destroyed when the function returns
local has automatic storage duration and is destroyed when getValue returns, so the returned reference dangles. Compilers usually warn, but the code compiles, and using the result is undefined behaviour. Returning by value (int) or returning a reference to something that outlives the call, such as a member or a static object, fixes the problem.
Answer checked How we write and check questions
Discussion