2

Embedded types like int, char etc could be returned from function using registers, but what if function return some large object. I suggest process's stack couldn't be used for such issue, am I right? Could anyone explain how does object passed to callee?

SomeObj function() {
    SomeObj someObj;
    return someObj;
}
vard
  • 2,142
  • 4
  • 30
  • 40
  • Why would stack not work?! Stack limits are OS dependant also most large object create theor data on heap and the object it self on stack. – Karim Tarabishy Nov 18 '14 at 22:34
  • @Doggynub could you specify particular moment in function calling/exiting when return value is pushed on stack, please? – vard Nov 18 '14 at 22:38
  • You should specify what architecture you are asking about. Also, read the appropriate documentation. – Jester Nov 18 '14 at 22:39
  • 1
    Here's [how the Itanium ABI prescribes it](https://mentorembedded.github.io/cxx-abi/abi.html#calls). – Kerrek SB Nov 18 '14 at 22:40

1 Answers1

3

Normally in most compilers the stack is used for this purpose. Even for large objects it is still the case. The calling function expects to find the called function value on the stack when the called function returns.

One of the solutions is to reserve the stack area before the function gets called. The called function may access this area to fill it with result (the access to the stack is not limited to the top after all).

Wojtek Surowka
  • 20,535
  • 4
  • 44
  • 51
  • Doesn't callee return address lie deeper than any value you can put in stack in called function? Does called function pop callee return address from stack, then put object it wants to return and then perform jump to popped address? – vard Nov 18 '14 at 22:34
  • This is handled by compilers to have a pointer in the called stack referring to some place in calLee stack where returned data is moved their – Karim Tarabishy Nov 18 '14 at 22:37
  • One of the solutions is to reserve the stack area before the function gets called. The called function may access this area to fill it with result (the access to the stack is not limited to the top after all). – Wojtek Surowka Nov 18 '14 at 22:38
  • Computer memory is random-accessible. "Deeper" is not an obstacle -- just add or subtract a bunch of bytes from the stack frame as needed. – Kerrek SB Nov 18 '14 at 22:38