Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Returning local variables without copying them [duplicate]

Tags:

c++

I'm new to c++ and would like to know if there is a way to create an object in a function and then return that object without it having to be copied in memory. I know the original local object goes out of scope, but I hoped the compiler could optimize it in a way that the copy of the object would just reuse the same memory adress.

int foo()
{
    int bar = 5;
    std::cout << &bar << std::endl;

    return bar;
}

int main()
{
    char a;
    auto b = foo();
    std::cout << &b << std::endl;
    std::cin >> a;
    return 0;
}

This returns different memory addresses. Since the address of bar is no longer needed and b is always the same size, is there any reason it couldn't just use the same address and save the step of copying?


It doesn't really matter for a simple integer but for larger Objects, what would be the preferred way to make sure the returned object does not get copied in memory?

Would the following be a good approach?

int & foo(int & bar)
{
    std::cout << &bar << std::endl;
    // do things with bar
    return bar;
}

int main()
{
    char a;
    auto bar = 5;
    auto & b = foo(bar);
    std::cout << &b << std::endl;
    std::cin >> a;
    return 0;
}

As a side question, why does the following thing work:

int & foo()
{
    int bar = 5;
    std::cout << &bar << std::endl;

    return bar;
}

int main()
{
    char a;
    auto & b = foo();
    std::cout << &b << " " << b << std::endl;
    std::cin >> a;
    return 0;
}

I expected that bar is out of scope and could no longer be referred to, but this compiles and returns the correct value (MSVC++ 2013).

like image 393
BoshWash Avatar asked Mar 14 '15 11:03

BoshWash


People also ask

Can local variables be returned?

How to return a local variable from a function? But there is a way to access the local variables of a function using pointers, by creating another pointer variable that points to the variable to be returned and returning the pointer variable itself.

Why we should not return a pointer to a local variable?

The return statement should not return a pointer that has the address of a local variable ( sum ) because, as soon as the function exits, all local variables are destroyed and your pointer will be pointing to someplace in the memory that you no longer own.

How do I copy a local variable in Labview?

Because a local variable must always have a control or indicator associated with it, these objects are pasted with the local variable, regardless of whether or not they are pasted in the same VI. CTRL+C and CTRL+V - this method is the classic copy and paste method used to perform many operations in Windows.

Can you change a reference c++?

You can't reassign a reference, but if you're looking for something that would provide similar abilities to this you can do a pointer instead.


2 Answers

It doesn't really matter for a simple integer but for larger Objects, what would be the preferred way to make sure the returned object does not get copied in memory?

Just return the object. C++ has return value optimization which means the copy can be (and most often is) elided:

big_object get_object()
{
    big_object bo;
    ....
    return bo;
}

....

big_object big = get_object(); // big gets built in-place

If you look at the address of bo inside the function and big outside, you would expect to see the same address (although RVO is an optimization that can happen, but doesn't have to, so there are other factors that could influence whether you see the same address. With recent g++ and clang++ I get the same address for objects of size 8 bytes, but not for size 4.) For example, this produces the same address on clang++ 3.5:

#include <iostream>

struct foo { int i[2]; };

foo make_foo()
{
  foo f;
  std::cout << &f << std::endl;
  return f;
}

int main()
{
  foo f = make_foo();
  std::cout << &f << std::endl;
}
like image 60
juanchopanza Avatar answered Sep 30 '22 08:09

juanchopanza


If your compiler follows the C++ standard, there's no need to use strange code: the compiler is in fact allowed (according to the standard) to optimize the returning of values from functions, omitting any copy (just Google "c++ return value optimization"). This way, you can even return big structures on the stack by value, without worrying.

EDIT: Answer corrected, thanks to juanchopanza.

like image 26
lodo Avatar answered Sep 30 '22 10:09

lodo