0

Let's have a look to this code. I was thinking a c++ function only returns an int value in eax. So where does it store the vector result ? Heap ? Stack ?

Thanks

#include <iostream>
#include <vector>

using namespace std;


vector<int> fonction(int a)
{
        vector<int> vec;
        for (int i=0;i<a;i++)
        {
                vec.push_back(1);
        }
        return vec;
}

int main(int argc, char *argv[])
{
        cout << "test" << endl;
        auto res = fonction(10);
        cout << res.size() << endl;
        return 0;
}
Slava
  • 43,454
  • 1
  • 47
  • 90
Bob5421
  • 7,757
  • 14
  • 81
  • 175
  • With C++11+ your the returning vector of `function` is move-constructed into `res`. – MrTux Jun 30 '17 at 12:43
  • 1
    The register used to store the return value is not an attribute of C++, but rather the architecture that you're running on. – Attie Jun 30 '17 at 12:43
  • depends on the compiler. Modern compilers perform "copy elision" to save a copy. They assign the value directly in the `res` variable. – Jean-François Fabre Jun 30 '17 at 12:43
  • 1
    "Heap" and "Stack" are not part of the C++ language, they are implementation details – UnholySheep Jun 30 '17 at 12:44
  • In terms of assembly code, it really depends on a number of factors (C++ language version, compiler vendor and version, target architecture, optimisation settings, etc.) –  Jun 30 '17 at 12:44
  • okay, is it correct to return a vector type from a function ? – Bob5421 Jun 30 '17 at 12:44
  • @Bob5421 it is not only correct, but recommended way (rather than passing vector by pointer or reference to be modified). – Slava Jun 30 '17 at 13:29

2 Answers2

2

As far as registers go the C++ standard does not specify which registers should contain the value, since the standard is not dependent on architectures. It just defines the syntax and semantics of the language

As for what happens when you create the vector in your function, the contents of the vector are stored on the heap (as usual) and the vector itself (i.e. the pointer and other bookkeeping) is stored in the stack frame of the function.

And when you go to return the vector from the function by value, the return value is treated as an rvalue, and that rvalue is guaranteed to be moved into the vector (i.e. the pointer and other bookkeeping) that has been allocated in main.

Note that there is something called NRVO, which if the compiler is able to apply. There is no moving, the value is simply taken from the stack of the function and put where it needs to go.


okay, is it correct to return a vector type from a function ?

Absolutely, it's completely fine

Curious
  • 20,870
  • 8
  • 61
  • 146
  • 1
    You can return an `std::array` but not a regular array, see more https://stackoverflow.com/questions/3473438/return-array-in-a-function – Curious Jun 30 '17 at 12:57
  • @Bob5421 just a rule of the language, nothing special about it – Curious Jun 30 '17 at 13:01
  • @Bob5421 simply put: a vector can be copied (and moved), an array can't. When returning a vector it is copied/moved to the caller and the local vector is destroyed. When returning an array, it decays to a pointer to the first element, which is returned to the caller. Then the local array is destroyed, and the returned pointer points to freed memory – king_nak Jun 30 '17 at 13:07
  • @Bob5421 It's pretty much the same reason that arrays can't be assigned. C++ arrays are part of the memory and object model inherited from C, where there is no general way to discover the size of an array. – Mike Housky Jun 30 '17 at 13:12
0

looks like gcc forx86_64allocates return vector struct on stack of the caller and returns a pointer to it in rax.

movq    -48(%rbp), %rax
...
ret
Serge
  • 11,616
  • 3
  • 18
  • 28