D
Divick
I was reading this section in Bruce Eckel's book which talks about
passing and returning large objects ( Chapter 11: References & the
Copy-Constructor ), where he explains that how to return big objects /
values from functions and the issues involved therein specifically
w.r.t. reentrency.
He explains that because of reentrency, the object to be returned can't
be located down in the stack (down in relative here, which means that
stack is growing downwards) as an interrupt can come along when you are
returning from a function call, which could override the return value.
And also it can't be returned in a global area (for obvious reasons of
overriding the return value in global location on reenterent call or
for recursion) .
Then he concludes that the only safe place to return values in
registers but registers not being large enough for storing values. Thus
"The answer is to push the address of the return value's destination
on the stack as one of the function arguments, and let the function
copy the return information directly into the destination. This not
only solves all the problems, it's more efficient." (Quote from the
book).
So the problem that I see in the above explanation is that even though
the compiler makes extra space for return value in function arguments
and the callee function pushes the address of object to be returned in
this location, but after all the object is located on to the stack
(being a local object which is being returned by value) and thus by the
same logic of reenterant call, this object's value could be overridden
when an interrupt comes along when the object is being returned from
the function.
Thus how does making extra space in the function arguments for a return
address fix this problem of returning objects be value?
I hope I am clear in my explanation.
Thanks,
Divick
passing and returning large objects ( Chapter 11: References & the
Copy-Constructor ), where he explains that how to return big objects /
values from functions and the issues involved therein specifically
w.r.t. reentrency.
He explains that because of reentrency, the object to be returned can't
be located down in the stack (down in relative here, which means that
stack is growing downwards) as an interrupt can come along when you are
returning from a function call, which could override the return value.
And also it can't be returned in a global area (for obvious reasons of
overriding the return value in global location on reenterent call or
for recursion) .
Then he concludes that the only safe place to return values in
registers but registers not being large enough for storing values. Thus
"The answer is to push the address of the return value's destination
on the stack as one of the function arguments, and let the function
copy the return information directly into the destination. This not
only solves all the problems, it's more efficient." (Quote from the
book).
So the problem that I see in the above explanation is that even though
the compiler makes extra space for return value in function arguments
and the callee function pushes the address of object to be returned in
this location, but after all the object is located on to the stack
(being a local object which is being returned by value) and thus by the
same logic of reenterant call, this object's value could be overridden
when an interrupt comes along when the object is being returned from
the function.
Thus how does making extra space in the function arguments for a return
address fix this problem of returning objects be value?
I hope I am clear in my explanation.
Thanks,
Divick