Connecting Tech Pros Worldwide Help | Site Map

inline functions and return by reference.

Sreenivas
Guest
 
Posts: n/a
#1: Jul 22 '05
Hi,
We cannot return a reference to an automatic variable from a function, as per
the ANSI C++ standard the behaviour is undefined. Does this hold for inline
functions too? or can I return a reference to the automatic variable from the
inline function. I meant automatic variable by stack variable strictly local to
that function.
Thanks,
Sreenivas.
John Harrison
Guest
 
Posts: n/a
#2: Jul 22 '05

re: inline functions and return by reference.



"Sreenivas" <sreenu30@gmail.com> wrote in message
news:e6f5a454.0410192229.1d56f442@posting.google.c om...[color=blue]
> Hi,
> We cannot return a reference to an automatic variable from a function, as
> per
> the ANSI C++ standard the behaviour is undefined. Does this hold for
> inline
> functions too?[/color]

Yes

john


JKop
Guest
 
Posts: n/a
#3: Jul 22 '05

re: inline functions and return by reference.


Sreenivas posted:
[color=blue]
> Hi,
> We cannot return a reference to an automatic variable from a function,
> as per the ANSI C++ standard the behaviour is undefined. Does this hold
> for inline functions too? or can I return a reference to the automatic
> variable from the inline function. I meant automatic variable by stack
> variable strictly local to that function.
> Thanks,
> Sreenivas.[/color]


The following two functions are equivalent in that they give the caller
access to an object which has already been destroyed:

int& Monkey()
{
int k = 4;

return k;
}


int& Ape()
{
int k = 4;

return &k;
}



As regards inline functions, they work exactly as do everyday functions.
Their static variables work exactly the same too. There's only two
differences between an inline function and a normal function:

1) It has "inline" written before it.

2) You're not violating the One Definition Rule if you define it more than
once, eg. the following is *il*legal:

int Blah() { return 5; }
int Blah() { return 5; }

While the following is perfectly legal:

inline int Blah() { return 5; }
inline int Blah() { return 5; }


(Or maybe I'm mistaken there? Perhaps you can define inline functions as
many times as you want... except it must be once per translation unit?)

Anyway, the result is that you can stick an inline function in a header
file!


-JKop
Michael Kurz
Guest
 
Posts: n/a
#4: Jul 22 '05

re: inline functions and return by reference.



"JKop" <NULL@NULL.NULL> schrieb im Newsbeitrag
news:LTndd.39556$Z14.13981@news.indigo.ie...[color=blue]
> Sreenivas posted:
>
>
> int Blah() { return 5; }
> int Blah() { return 5; }
>
> While the following is perfectly legal:
>
> inline int Blah() { return 5; }
> inline int Blah() { return 5; }
>
>
> (Or maybe I'm mistaken there? Perhaps you can define inline functions as
> many times as you want... except it must be once per translation unit?)
>[/color]

IMHO "inline" solves only the linker error of linking multiple functions
with the same signature, as the compiler and the linker will typically keep
track of them over all compilation units, whereas including the same
function in the same compilation unit twice leads to a compiler error.


Regards
Michael




Sreenivas
Guest
 
Posts: n/a
#5: Jul 22 '05

re: inline functions and return by reference.


"John Harrison" <john_andronicus@hotmail.com> wrote in message news:<2tmevuF215g2lU1@uni-berlin.de>...[color=blue]
> "Sreenivas" <sreenu30@gmail.com> wrote in message
> news:e6f5a454.0410192229.1d56f442@posting.google.c om...[color=green]
> > Hi,
> > We cannot return a reference to an automatic variable from a function, as
> > per
> > the ANSI C++ standard the behaviour is undefined. Does this hold for
> > inline
> > functions too?[/color]
>
> Yes
>
> john[/color]

const int& junk()
{
const int junk = 11;
return junk;
}

int main(...)
{
int basket; // const int & basket = junk(); __This cud give unexpected
// behaviour__

basket = junk(); // but what about this????????
return 0;
}

does this code snippet give the expected bahaviour all the time?
can i rely on this code??
-Sreenivas
John Harrison
Guest
 
Posts: n/a
#6: Jul 22 '05

re: inline functions and return by reference.


>[color=blue]
> const int& junk()
> {
> const int junk = 11;
> return junk;
> }
>
> int main(...)
> {
> int basket; // const int & basket = junk(); __This cud give unexpected
> // behaviour__
>
> basket = junk(); // but what about this????????
> return 0;
> }
>
> does this code snippet give the expected bahaviour all the time?
> can i rely on this code??
> -Sreenivas[/color]

I don't know, I would ask this question again where someone who does know
can answer it.

john


Sreenivas
Guest
 
Posts: n/a
#7: Jul 22 '05

re: inline functions and return by reference.


"John Harrison" <john_andronicus@hotmail.com> wrote in message news:<2tp3i0F21hm2iU1@uni-berlin.de>...[color=blue][color=green]
> >
> > const int& junk()
> > {
> > const int junk = 11;
> > return junk;
> > }
> >
> > int main(...)
> > {
> > int basket; // const int & basket = junk(); __This cud give unexpected
> > // behaviour__
> >
> > basket = junk(); // but what about this????????
> > return 0;
> > }
> >
> > does this code snippet give the expected bahaviour all the time?
> > can i rely on this code??
> > -Sreenivas[/color][/color]

Would someone acknowledge this? whether this explanation is correct or
not.

when junk() returns a reference to an object, infact it's value wud b
copied into _basket_, as _basket_ is an object itself and not a
reference and it has it's own memory allocated. so, if this statement
is executed as an atomic statement(in multi-threaded env) it shouldn't
give any problems as per my understanding.(what happens if it's single
threaded env? i see no problems)

PS: to understand the above explanation refer the code above
John Harrison
Guest
 
Posts: n/a
#8: Jul 22 '05

re: inline functions and return by reference.



"Sreenivas" <sreenu30@gmail.com> wrote in message
news:e6f5a454.0410210301.3a69242e@posting.google.c om...[color=blue]
> "John Harrison" <john_andronicus@hotmail.com> wrote in message[/color]
news:<2tp3i0F21hm2iU1@uni-berlin.de>...[color=blue][color=green][color=darkred]
> > >
> > > const int& junk()
> > > {
> > > const int junk = 11;
> > > return junk;
> > > }
> > >
> > > int main(...)
> > > {
> > > int basket; // const int & basket = junk(); __This cud give[/color][/color][/color]
unexpected[color=blue][color=green][color=darkred]
> > > // behaviour__
> > >
> > > basket = junk(); // but what about this????????
> > > return 0;
> > > }
> > >
> > > does this code snippet give the expected bahaviour all the time?
> > > can i rely on this code??
> > > -Sreenivas[/color][/color]
>
> Would someone acknowledge this? whether this explanation is correct or
> not.
>
> when junk() returns a reference to an object, infact it's value wud b
> copied into _basket_, as _basket_ is an object itself and not a
> reference and it has it's own memory allocated. so, if this statement
> is executed as an atomic statement(in multi-threaded env) it shouldn't
> give any problems as per my understanding.(what happens if it's single
> threaded env? i see no problems)
>
> PS: to understand the above explanation refer the code above[/color]

I think the code is wrong. The reference refers to an object which has been
destroyed. Multithreading or single threading has nothing to do with it.

john


JKop
Guest
 
Posts: n/a
#9: Jul 22 '05

re: inline functions and return by reference.


[color=blue]
> const int& junk()
> {
> const int junk = 11;
> return junk;
> }[/color]

Returning a reference to a local object, giving the calling function access
to an object which no longer exists. Let me know how that goes!

Maybe use of the "auto" keyword would make it more obvious to you:

int const& junk()
{
int const auto junk = 1;

return junk;
}

[color=blue]
> int main(...)[/color]


No comment.

[color=blue]
> {
> int basket; // const int & basket = junk(); __This cud give
> unexpected
> // behaviour__
>
> basket = junk(); // but what about this????????[/color]


Here you're doing an assignment from an object that no longer exists. Let me
know how it goes!

[color=blue]
> return 0;
> }
>
> does this code snippet give the expected bahaviour all the time?
> can i rely on this code??[/color]


Undefined Behaviour.


-JKop
Closed Thread