473,404 Members | 2,187 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

Problem understanding how objects are returned by value

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

Sep 18 '06 #1
3 2119
Divick wrote:
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?
That's a bunch of baloney. (It's also platform-specific, so there might be
newsgroups that can detect the grain of truth within the baloney.)

On architectures with interrupts, and without virtual address spaces in
their CPUs, interrupts do indeed borrow your stack, and write "down" on it,
into the same space as your called functions could use.

However, any object returned by value is written into stack space "above"
your called function. A secret pointer to it goes into the function, and it
writes there. (Look up "return value optimization" to cover advanced
versions of this system.)

So you don't need to worry about such things, and you can return objects by
value freely, even on platforms with old-fashioned style interrupts and
hence old-fashioned style threading. Modern CPUs provide virtual contexts
that swap in and out automatically, so your stack is never borrowed. I
think!

--
Phlip
http://www.greencheese.us/ZeekLand <-- NOT a blog!!!
Sep 18 '06 #2

Divick wrote:
[snip]
(Bruce Eckel quote:)
"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).
[snip]
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.
I believe you were not! I agree with Philip that most of your post was
difficult to understand. But the Eckel quote above looks as if it is
simply a description of the "return value optimization". Look up this
(and perhaps also "named return value optimization") and feel free to
ask any questions if you still do not understand.

/Peter

Sep 18 '06 #3
peter koch wrote:
I believe you were not! I agree with Philip that most of your post was
difficult to understand. But the Eckel quote above looks as if it is
simply a description of the "return value optimization". Look up this
(and perhaps also "named return value optimization") and feel free to
ask any questions if you still do not understand.
Well I have got the answer to my question. Basically the explanation is
very subtle. To understand the question, first try to figure out how
would a function return an object by value keeping reentrency in mind.
Once the question is clear, I can try to explain once again what Bruce
meant (in the quote from his book). He could have made it conspicuous
by just using "caller" and "callee" instead of saying "function".

Thanks anyway,
Divick

Sep 19 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

49
by: Steven Bethard | last post by:
I promised I'd put together a PEP for a 'generic object' data type for Python 2.5 that allows one to replace __getitem__ style access with dotted-attribute style access (without declaring another...
4
by: scott cooper | last post by:
I hope this illustrates my question a little better. Consider the following script; why does the client side script change the div object's innerHtml property but the server side script does not?...
4
by: Leslaw Bieniasz | last post by:
Cracow, 20.09.2004 Hello, I need to implement a library containing a hierarchy of classes together with some binary operations on objects. To fix attention, let me assume that it is a...
15
by: Eric J. Holtman | last post by:
I feel like the answer to this should be blinding obvious. I also feel like it's probably an exercise in an undergraduate database design course. I'm off to google for an answer, but I figure I'd...
4
by: Bangalore | last post by:
Hi all, I am finding quite difficulty in understanding the behaviour of the following program. Base class is singleton, so it should allow the creation of only one object. Eventhough it is...
25
by: Xah Lee | last post by:
Python Doc Problem Example: gzip Xah Lee, 20050831 Today i need to use Python to compress/decompress gzip files. Since i've read the official Python tutorial 8 months ago, have spent 30...
3
by: Miguel | last post by:
Hi, I'm new to .NET and am using VB .NET as I am from a VB background. I am having difficulty understanding the way .NET handles, passes and uses objects. I have had a look at the Micrsoft Data...
13
by: Jeroen | last post by:
Hi all, I'm trying to implement a certain class but I have problems regarding the copy ctor. I'll try to explain this as good as possible and show what I tried thusfar. Because it's not about a...
2
by: VJ | last post by:
I tried to write sample code to get understanding of javascript's prototypal inheritance (along with the variety of function calling choices.. ) During the process, I got myself throughly...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.