What happens if a function returns the address of one of its local variables?
-
A
The compiler promotes the variable to global
-
B
The program stops with a runtime error
-
C
Using that pointer is undefined behaviour
Correct answer
-
D
The value is moved to the heap automatically
Correct answer
C. Using that pointer is undefined behaviour
14 views
Explanation
The local object's lifetime ends on return, so the pointer dangles and any use of it is undefined behaviour.
Reference
Asked in: NTS, PPSC IT, Campus Interviews
Doubts & discussion
Not sure why the answer is C? Ask here and get an explanation.
Ask a doubt about this MCQ