Within the body of the function, the return statement should NOT return a pointer or a reference that has the 220-601 address in memory of a local variable that was declared within the function, because as soon as the function exits, all local variables are destroyed and your pointer or reference will be pointing to some place in memory which you no longer own, so you cannot guarantee its contents. If the object to which a pointer refers is destroyed, the pointer is said to 220-602 be a dangling pointer until it is given a new value; any use of the value of such a pointer is invalid. Having a dangling pointer like that is dangerous; pointers or references to local variables must not be allowed to escape the function in which those local (aka automatic) variables live.
However, within the body of your function, if your pointer or reference has the address in memory of a N10-003 data type, struct, or class that you dynamically allocated the memory for, using the new operator, then returning said pointer or reference would be reasonable:
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment