473,378 Members | 1,617 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,378 software developers and data experts.

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 1585
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Anthony | last post by:
Hello, I am writing a function that populates an array of pointers to strings. Both the number of strings in the array, and the lengths of the strings, are dynamic; in particular, the number of...
6
by: Generic Usenet Account | last post by:
Is it okay to return a local datastructure (something of type struct) from a function, as long as it does not have any pointer fields? I think it is a bad idea, but one of my colleagues does not...
17
by: I.M. !Knuth | last post by:
Hi. I'm more-or-less a C newbie. I thought I had pointers under control until I started goofing around with this: ...
17
by: djcredo | last post by:
Hey all, I want to return a pointer to a struct. Here is what I'lm trying to do: struct Position{ int x; int y; }; Position* GraphicTag::getRenderCentre(){
6
by: student1976 | last post by:
All Beginner/Intermediate level question. I understand that returning ptr to local stack vars is bad. Is returning foo_p_B from fnB() reliable all the time, so that using foo_p_A does not...
3
by: John Turner | last post by:
typedef void (*vfp)(); typedef vfp (*fp)(); static fp hello() { printf("Hello.\n"); return (fp)&hello; } main(){
17
by: daniel | last post by:
Hello , How safe is it to return a structure from a function. Let's say we have the following situation: typedef struct MyStruct{ int a;
160
by: DiAvOl | last post by:
Hello everyone, Please take a look at the following code: #include <stdio.h> typedef struct person { char name; int age; } Person;
5
by: ctj951 | last post by:
I have a very specific question about a language issue that I was hoping to get an answer to. If you allocate a structure that contains an array as a local variable inside a function and return...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...

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.