Returning reference of a temporary object from a function

c++ const reference to temporary
reference to local variable returned
c++ return reference to member
return by reference in c++ - geeksforgeeks
c++ return const reference
functions that return a reference c++
c return rvalue reference
c++ function return string example

Consider the following code -

#include <iostream>
#include <stdio.h>

const int & retRef() {
    return 6;
}

int main()
{
    const int& k = retRef();
    printf("Value: %d\n", k);
    printf("Address: %p\n", &k);
    printf("Value: %d\n", k);
    return 0;
}

The output is -

Value: 6
Address: 0x7ffd45bf544c
Value: 32692

Why the value changed after printing the address of the variable k? If I replace the line const int& k = retRef() with const int& k = 6;, the output is as expected.

Why is this different behavior? Thanks in advance

Jacek's C++ Blog · Const References to Temporary Objects, "However, the case for temporary objects passed to functions is actually an exception: The exceptions to this lifetime rule are: A temporary object bound to a​  The function must be declared to return a reference, and a reference has to refer to an object that will continue to exist after the function exits. Your temporary "AnotherObject()" is destructed right after the return, so that obviously won't work.

You are returning a reference to a temporary object which will cause undefined behaviour. The object will not be available once function returns.

n4659 - § 15.2:

(6.2) — The lifetime of a temporary bound to the returned value in a function return statement (9.6.3) is not extended; the temporary is destroyed at the end of the full-expression in the return statement.

7.4a, How do you return an object from a function in Python? Functions can be declared to return a reference type. There are two reasons to make such a declaration: The information being returned is a large enough object that returning a reference is more efficient than returning a copy. The type of the function must be an l-value. The referred-to object will not go out of scope when the function returns. Just as it can be more efficient to pass large objects to functions by reference, it also can be more efficient to return large objects from

In function retRef you are returning a reference to a local variable. The objects inside the function will be destroyed when exiting the function and all references to it will become invalid. Using of that link will further entail undefined behavior...

const int & retRef()
{
    return 6;
}

Return by Reference | CPP, What is returned by functions that don't have a return statement? A function can also return objects either by value or by reference. When an object is returned by value from a function, a temporary object is created within the function, which holds the return value. This value is further assigned to another object in the calling function. The syntax for defining a function that returns an object by value is

How to return an object from a function in Python?, C++ Tutorial: Object Returning, When a function, either a member function or a The function could return: A reference to an object, A constant reference to an object, and the function shouldn't return a reference to a temporary object, either. Since you're not in control of the return type, you must make sure you return a valid object and not just a temporary. One solution would be a function-local static variable: virtual const core::matrix4& getViewMatrixAffector() const { static const core::matrix4 val; return val; }; If you find yourself doing this in many functions (with the same type of the variable), make val a (suitably renamed) static member of the class.

Python Fundamentals Tutorial: Functions, has written the following function, which uses unnecessary temporary objects this a reference return without changing the function's semantics by declaring  Return by reference. Similar to pass by address, values returned by reference must be variables (you should not return a reference to a literal or an expression that resolves to a temporary value, as those will go out of scope at the end of the function and you’ll end up returning a dangling reference).

C++ Tutorial: Object Returning - 2020, In this case, at() returns a temporary, which you cannot pass on using a reference​. The solution is to declare your function as follows: template <class S, class T>  Consider the following function: const Point multiply(const Point& p); The function above returns an object by value. From the previous tip, you know that you should not pass an object by value, but instead by reference. Passing by reference means passing a reference to an object that already exist. Thus, if you want to return a reference in a function, it means that you must create that object in the function.

Comments
  • To nitpick a little: When using the "%p" format with printf, the correct argument type is void*. Casting is really needed to avoid undefined behavior. Also, why use printf instead of std::cout?
  • The variable that you are trying to access is already cleared by system from the stack, because when the function end, its clear local variable inside that function.
  • So, this will be undefined behavior irrespective of const int& or int&&, right?
  • @kuro Yes, it's irrespective.