470,833 Members | 1,389 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,833 developers. It's quick & easy.

Returning a Struct from a Function

Hello, newbie here.

I'd like to know if there is a way to return a structure from a
function, without creating any other intermediate variables. Also,
this should be done such that the function can still be declared as
inline.

More precisely, given the structure:

struct TwoFields {
int Field_one;
int Field_two;
}

I want a function like the following one, but without the
extra declaration, "TwoFields myReturnStruct;":

inline TwoFields ReturnMyFields(int myInt) {

TwoFields myReturnStruct;

myReturnStruct.Field_one = myInt/2;
myReturnStruct.Field_two = myInt%2;

return myReturnStruct;
}

The only reason I want this is speed efficiency, so if this is not
worth doing please let me know.

Alex
Jun 27 '08 #1
11 1437
al*******@gmail.com dixit:
Hello, newbie here.

I'd like to know if there is a way to return a structure from a
function, without creating any other intermediate variables. Also,
this should be done such that the function can still be declared as
inline.
The usual way is to pass a reference to the struct (as a parameter) in
which you want to put the return value (the same for vector, list, ...)
Jun 27 '08 #2
On May 11, 2:02 pm, john <j...@gmail.comwrote:
alex.j...@gmail.com dixit:
Hello, newbie here.
I'd like to know if there is a way to return a structure from a
function, without creating any other intermediate variables. Also,
this should be done such that the function can still be declared as
inline.

The usual way is to pass a reference to the struct (as a parameter) in
which you want to put the return value (the same for vector, list, ...)
OK, that certainly works.

But is this the only way to do it?

What if I have a lot of code which returns a struct (or vector,
etc..) by creating an intermediate variable in the function; and I do
not want to modify all the occurrences of the function in the code,
only the function itself.

Can I eliminate the creation of the extra variable, while returning
the struct (but not as a parameter) ?

Also, thanks for the quick reply.

Alex
Jun 27 '08 #3
On 11 Maj, 19:57, alex.j...@gmail.com wrote:
Hello, newbie here.

* I'd like to know if there is a way to return a structure from a
function, without creating any other intermediate variables. Also,
this should be done such that the function can still be declared as
inline.
Yes. Any ordinary function will likely do so. The compiler is allowed
to optimise this special case that has been given the name (N)RVO for
(Named) Return Value Optimisation and all recent compilers should be
able to use this strategy.
>
* More precisely, given the structure:

struct TwoFields {
* *int Field_one;
* *int Field_two;

}

I want a function like the following one, but without the
extra declaration, "TwoFields myReturnStruct;":

inline TwoFields ReturnMyFields(int myInt) {

TwoFields myReturnStruct;

myReturnStruct.Field_one = myInt/2;
myReturnStruct.Field_two = myInt%2;

return myReturnStruct;

}
This looks fine to me.
* The only reason I want this is speed efficiency, so if this is not
worth doing please let me know.
I can sympathise with your attitude - I used to think that way
myself. But seriously: do you believe that the difference in execution
time for that function is going to make any difference?

/Peter
Jun 27 '08 #4
On May 11, 2:19 pm, peter koch <peter.koch.lar...@gmail.comwrote:
On 11 Maj, 19:57, alex.j...@gmail.com wrote:
Hello, newbie here.
I'd like to know if there is a way to return a structure from a
function, without creating any other intermediate variables. Also,
this should be done such that the function can still be declared as
inline.

Yes. Any ordinary function will likely do so. The compiler is allowed
to optimise this special case that has been given the name (N)RVO for
(Named) Return Value Optimisation and all recent compilers should be
able to use this strategy.

If I parse this correctly, you are saying that a good compiler will
not actually make the extra allocation if it is not needed, even if I
use the code in the initial post. Is that correct?

The only reason I want this is speed efficiency, so if this is not
worth doing please let me know.

I can sympathise with your attitude - I used to think that way
myself. But seriously: do you believe that the difference in execution
time for that function is going to make any difference?
I genuinely do not know, but I thought it a good thing to know.
I do use the function deep inside nested loops in a computation
that takes days if not weeks, so I am trying to speed up genuine
bottlenecks, not just doing it for the sake of frivolous efficiency.

Thank you for the reply,
Alex

Jun 27 '08 #5
al*******@gmail.com wrote:
Hello, newbie here.

I'd like to know if there is a way to return a structure from a
function, without creating any other intermediate variables. Also,
this should be done such that the function can still be declared as
inline.

More precisely, given the structure:

struct TwoFields {
int Field_one;
int Field_two;
}

I want a function like the following one, but without the
extra declaration, "TwoFields myReturnStruct;":

inline TwoFields ReturnMyFields(int myInt) {

TwoFields myReturnStruct;

myReturnStruct.Field_one = myInt/2;
myReturnStruct.Field_two = myInt%2;

return myReturnStruct;
}

The only reason I want this is speed efficiency, so if this is not
worth doing please let me know.
Premture optimization alert !

BTW - if you really want to do this - imho this is the easy way - and
BTW - RVO will prolly happen.

TwoFields & f = ReturnMyFields(v);

Notice there is a temporary returned which is getting bound to a
reference. In theory, this will only get constructed once - in the
function ReturnMyFields; If your compiler is really smart, (and olot of
them are) it will never hit memory and exist only in registers.

Unless you "know" this is an issue for you, don't bother worrying about it.
Jun 27 '08 #6
On 2008-05-12 00:33, Gianni Mariani wrote:
al*******@gmail.com wrote:
>Hello, newbie here.

I'd like to know if there is a way to return a structure from a
function, without creating any other intermediate variables. Also,
this should be done such that the function can still be declared as
inline.

More precisely, given the structure:

struct TwoFields {
int Field_one;
int Field_two;
}

I want a function like the following one, but without the
extra declaration, "TwoFields myReturnStruct;":

inline TwoFields ReturnMyFields(int myInt) {

TwoFields myReturnStruct;

myReturnStruct.Field_one = myInt/2;
myReturnStruct.Field_two = myInt%2;

return myReturnStruct;
}

The only reason I want this is speed efficiency, so if this is not
worth doing please let me know.

Premture optimization alert !

BTW - if you really want to do this - imho this is the easy way - and
BTW - RVO will prolly happen.

TwoFields & f = ReturnMyFields(v);
const TwoFields & f = ReturnMyFields(v);

--
Erik Wikström
Jun 27 '08 #7
Stefan Ram wrote:
=?UTF-8?B?RXJpayBXaWtzdHLDtm0=?= <Er***********@telia.comwrites:
>>>inline TwoFields ReturnMyFields(int myInt) {
TwoFields myReturnStruct; [...]
return myReturnStruct;
const TwoFields & f = ReturnMyFields(v);

How long will the object created with »TwoFields myReturnStruct;«
live?

I used to believe that its lifetime would end not later than the
evaluation of the full expression »ReturnMyFields(v)« or even
when the control is leaving the block containing its declaration.
And what changed your mind? <g>

Actually, the temporary returned from the function, the object to
which 'f' is made to refer by means of initializing it, will live
as long as 'f' itself. That's one of the exceptions the the rule
for the "normal" lifetime of a temporary (end of the expression).

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 27 '08 #8
On 2008-05-12 18:42, Stefan Ram wrote:
=?UTF-8?B?RXJpayBXaWtzdHLDtm0=?= <Er***********@telia.comwrites:
>>>>inline TwoFields ReturnMyFields(int myInt) {
TwoFields myReturnStruct; [...]
return myReturnStruct;
const TwoFields & f = ReturnMyFields(v);

How long will the object created with »TwoFields myReturnStruct;« live?

I used to believe that its lifetime would end not later than the
evaluation of the full expression »ReturnMyFields(v)« or even
when the control is leaving the block containing its declaration.
When you bind a temporary to a const reference the lifetime of the
temporary is extended to last as long as the reference.

--
Erik Wikström
Jun 27 '08 #9
On May 12, 6:59 pm, Erik Wikström <Erik-wikst...@telia.comwrote:
On 2008-05-12 18:42, Stefan Ram wrote:
=?UTF-8?B?RXJpayBXaWtzdHLDtm0=?= <Erik-wikst...@telia.comwrites:
>>>inline TwoFields ReturnMyFields(int myInt) {
TwoFields myReturnStruct; [...]
return myReturnStruct;
const TwoFields & f = ReturnMyFields(v);
How long will the object created with »TwoFields
myReturnStruct;« live?
I used to believe that its lifetime would end not later
than the evaluation of the full expression
»ReturnMyFields(v)« or even when the control is leaving
the block containing its declaration.
When you bind a temporary to a const reference the lifetime of
the temporary is extended to last as long as the reference.
That can easily be misunderstood (and often is). Formally, the
rule is when the initialization expression of a reference is a
temporary, the lifetime of the temporary is extended to
correspond to the lifetime of that reference. Which isn't quite
the same thing.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jun 27 '08 #10
On May 12, 6:59 pm, Erik Wikström <Erik-wikst...@telia.comwrote:
When you bind a temporary to a const reference the lifetime of the
temporary is extended to last as long as the reference.
i'd just note that sun studio (at least v10) requires -
features=tmplife
option for this to work correctly, otherwise it will destroy the named
temporary at the end of the expression by default.
i spent few days on that one :)

mojmir
Jun 27 '08 #11
In message
<bc**********************************@i76g2000hsf. googlegroups.com>,
mojmir <sv*******@gmail.comwrites
>On May 12, 6:59 pm, Erik Wikström <Erik-wikst...@telia.comwrote:
>When you bind a temporary to a const reference the lifetime of the
temporary is extended to last as long as the reference.

i'd just note that sun studio (at least v10) requires -
features=tmplife
option for this to work correctly, otherwise it will destroy the named
temporary
What is a "named temporary"?
>at the end of the expression by default.
i spent few days on that one :)

mojmir
--
Richard Herring
Jun 27 '08 #12

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Generic Usenet Account | last post: by
17 posts views Thread by djcredo | last post: by
6 posts views Thread by student1976 | last post: by
3 posts views Thread by John Turner | last post: by
17 posts views Thread by daniel | last post: by
reply views Thread by mihailmihai484 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.