If a function returns a reference or a pointer to a local variable, the memory to which it refers will already have been deallocated, when this reference or pointer is used. Note that the scope in this rule refers to the lifetime-scope. It is never allowed that a function gives access to a local automatic stack variable. However, if the lifetime of a local variable exceeds the lifetime of its block, then it may well be appropriate to give access to it from other scopes. This might be the case for static or new allocated objects. |