return reference to local variable, good code?

Discussion in 'C++' started by George2, Dec 16, 2007.

  1. George2

    George2 Guest

    Hello everyone,


    I am reading some code from other people, there are some code like
    this,

    Code:
    class Foo {
    
    };
    
    Foo& func()
    {
    	Foo foo;
    	return foo;
    }
    
    int main()
    {
    	Foo& foo = func();
    
    	return 0;
    }
    
    I want to confirm with you that it is not good code, since we return a
    reference to local object instance, right? Even if the code has
    expected function currently.


    thanks in advance,
    George
     
    George2, Dec 16, 2007
    #1
    1. Advertising

  2. George2

    yanlinlin Guest

    On 12ÔÂ16ÈÕ, ÏÂÎç1ʱ49·Ö, George2 <> wrote:
    > Hello everyone,
    >
    > I am reading some code from other people, there are some code like
    > this,
    >
    >
    Code:
    > class Foo {
    >
    > };
    >
    > Foo& func()
    > {
    >         Foo foo;
    >         return foo;
    >
    > }
    >
    > int main()
    > {
    >         Foo& foo = func();
    >
    >         return 0;}
    >
    > 
    >
    > I want to confirm with you that it is not good code, since we return a
    > reference to local object instance, right? Even if the code has
    > expected function currently.
    >
    > thanks in advance,
    > George


    I'm afraid this code can not be compiled. To return a reference of
    local non-static object is not allowed.
     
    yanlinlin, Dec 16, 2007
    #2
    1. Advertising

  3. * George2:
    > Hello everyone,
    >
    >
    > I am reading some code from other people, there are some code like
    > this,
    >
    >
    Code:
    > class Foo {
    > 
    > };
    > 
    > Foo& func()
    > {
    > 	Foo foo;
    > 	return foo;
    > }
    > 
    > int main()
    > {
    > 	Foo& foo = func();
    > 
    > 	return 0;
    > }
    > 
    >
    > I want to confirm with you that it is not good code, since we return a
    > reference to local object instance, right? Even if the code has
    > expected function currently.


    It's Undefined Behavior if the function result is ever used.

    Btw., could you or anyone else please confirm that you can see this posting.

    It seems that all internet connections out of Norway are down at the
    moment, at least via my ISP (e.g. I can't reach Microsoft's servers, nor
    Google), yet articles keep dropping in in international newsgroups.

    Of course, if you don't see this posting, simply don't respond.

    Cheers, hth., & tia.,

    - Alf


    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, Dec 16, 2007
    #3
  4. * yanlinlin:
    > On 12月16æ—¥, 下åˆ1æ—¶49分, George2 <> wrote:
    >> Hello everyone,
    >>
    >> I am reading some code from other people, there are some code like
    >> this,
    >>
    >>
    Code:
    >> class Foo {
    >>
    >> };
    >>
    >> Foo& func()
    >> {
    >>         Foo foo;
    >>         return foo;
    >>
    >> }
    >>
    >> int main()
    >> {
    >>         Foo& foo = func();
    >>
    >>         return 0;}
    >>
    >> 
    >>
    >> I want to confirm with you that it is not good code, since we return a
    >> reference to local object instance, right? Even if the code has
    >> expected function currently.
    >>
    >> thanks in advance,
    >> George

    >
    > I'm afraid this code can not be compiled. To return a reference of
    > local non-static object is not allowed.


    It's allowed and it should compile.

    A compiler that doesn't allow it is non-conforming.

    However, using that result incurs Undefined Behavior.

    Cheers, & hth.,

    - Alf


    PS: Could you, or anyone, confirm whether you see this posting? If you
    don't see it, simply do nothing.


    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, Dec 16, 2007
    #4
  5. * Alf P. Steinbach:
    > * yanlinlin:
    >> On 12月16æ—¥, 下åˆ1æ—¶49分, George2 <> wrote:
    >>> Hello everyone,
    >>>
    >>> I am reading some code from other people, there are some code like
    >>> this,
    >>>
    >>>
    Code:
    >>> class Foo {
    >>>
    >>> };
    >>>
    >>> Foo& func()
    >>> {
    >>>         Foo foo;
    >>>         return foo;
    >>>
    >>> }
    >>>
    >>> int main()
    >>> {
    >>>         Foo& foo = func();
    >>>
    >>>         return 0;}
    >>>
    >>> 
    >>>
    >>> I want to confirm with you that it is not good code, since we return a
    >>> reference to local object instance, right? Even if the code has
    >>> expected function currently.
    >>>
    >>> thanks in advance,
    >>> George

    >>
    >> I'm afraid this code can not be compiled. To return a reference of
    >> local non-static object is not allowed.

    >
    > It's allowed and it should compile.
    >
    > A compiler that doesn't allow it is non-conforming.
    >
    > However, using that result incurs Undefined Behavior.
    >
    > Cheers, & hth.,
    >
    > - Alf
    >
    >
    > PS: Could you, or anyone, confirm whether you see this posting? If you
    > don't see it, simply do nothing.


    And I'm sorry this is EXTREMELY OFF-TOPIC, but perhaps someone knows how
    this can be?

    It seems the connection problems are protocol-dependent.

    Although I can't read my mail (GMail), I can read the New York Times,
    but pinging that server fails:

    ----------------------------------------------------------------------
    C:\Documents and Settings\Alf> wget www.nytimes.com
    --07:17:34-- http://www.nytimes.com/
    => `index.html'
    Resolving www.nytimes.com... 199.239.136.200, 199.239.136.245,
    199.239.137.200, ...
    Connecting to www.nytimes.com|199.239.136.200|:80... connected.
    HTTP request sent, awaiting response... 200 OK
    Length: unspecified [text/html]

    [ <=>
    ] 104 331 187.81K/s

    07:17:35 (187.31 KB/s) - `index.html' saved [104331]


    C:\Documents and Settings\Alf> ping www.nytimes.com

    Pinging www.nytimes.com [199.239.136.200] with 32 bytes of data:

    Request timed out.
    Request timed out.
    Request timed out.
    Request timed out.

    Ping statistics for 199.239.136.200:
    Packets: Sent = 4, Received = 0, Lost = 4 (100% loss),

    C:\Documents and Settings\Alf>_
    -------------------------------------------------------------------------

    If can see this posting, please follow-up, e.g. "off-topic Alf! Get lost!"

    Of course, if can't see it, simply do nothing.

    Cheers, and sorry for off-topic posting,

    - Alf

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, Dec 16, 2007
    #5
  6. George2

    Ian Collins Guest

    off-topic Alf! Get lost!

    Alf P. Steinbach wrote:
    > And I'm sorry this is EXTREMELY OFF-TOPIC, but perhaps someone knows how
    > this can be?
    >
    > It seems the connection problems are protocol-dependent.
    >
    > Although I can't read my mail (GMail), I can read the New York Times,
    > but pinging that server fails:
    >

    Is your PC infected by windows ??

    --
    Ian Collins.
     
    Ian Collins, Dec 16, 2007
    #6
  7. Re: off-topic Alf! Get lost!

    * Ian Collins:
    > Alf P. Steinbach wrote:
    >> And I'm sorry this is EXTREMELY OFF-TOPIC, but perhaps someone knows how
    >> this can be?
    >>
    >> It seems the connection problems are protocol-dependent.
    >>
    >> Although I can't read my mail (GMail), I can read the New York Times,
    >> but pinging that server fails:
    >>

    > Is your PC infected by windows ??


    Yeah, thanks for replying. Perhaps it's also OS-dependent... :) Just
    side-note on that while still very much off-topic: searching for causes
    I found a "DCOM startup" service running in spite of being designated as
    manual start, and since DCOM is Microsoft's super-duper entry vector for
    bad beasties (e.g. infamous RPC vulnerability some years ago) I tried to
    stop it but it wouldn't; I killed the process and Windows told me it had
    to shut down, which it did; and having disabled that service, when I
    then ran Word when Windows came up again, Word told me that "this
    document couldn't be registeted and therefore can't be linked to", i.e.
    Word silently invokes some DCOM -- network! -- registration when you
    open a document. Jeez. Shudder. Now, I'll take the punishment for OT.

    Cheers,

    - Alf

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, Dec 16, 2007
    #7
  8. George2

    Ian Collins Guest

    Re: off-topic Alf! Get lost!

    Alf P. Steinbach wrote:
    > Word silently invokes some DCOM -- network! -- registration when you
    > open a document. Jeez. Shudder. Now, I'll take the punishment for OT.
    >

    Repeat after me "I will install OpenOffice" "I will install OpenOffice"

    --
    Ian Collins.
     
    Ian Collins, Dec 16, 2007
    #8
  9. George2

    Salt_Peter Guest

    Re: off-topic Alf! Get lost!

    On Dec 16, 2:00 am, "Alf P. Steinbach" <> wrote:
    > * Ian Collins:
    >
    > > Alf P. Steinbach wrote:
    > >> And I'm sorry this is EXTREMELY OFF-TOPIC, but perhaps someone knows how
    > >> this can be?

    >
    > >> It seems the connection problems are protocol-dependent.

    >
    > >> Although I can't read my mail (GMail), I can read the New York Times,
    > >> but pinging that server fails:

    >
    > > Is your PC infected by windows ??

    >
    > Yeah, thanks for replying. Perhaps it's also OS-dependent... :) Just
    > side-note on that while still very much off-topic: searching for causes
    > I found a "DCOM startup" service running in spite of being designated as
    > manual start, and since DCOM is Microsoft's super-duper entry vector for
    > bad beasties (e.g. infamous RPC vulnerability some years ago) I tried to
    > stop it but it wouldn't; I killed the process and Windows told me it had
    > to shut down, which it did; and having disabled that service, when I
    > then ran Word when Windows came up again, Word told me that "this
    > document couldn't be registeted and therefore can't be linked to", i.e.
    > Word silently invokes some DCOM -- network! -- registration when you
    > open a document. Jeez. Shudder. Now, I'll take the punishment for OT.
    >
    > Cheers,
    >
    > - Alf
    >
    > --
    > A: Because it messes up the order in which people normally read text.
    > Q: Why is it such a bad thing?
    > A: Top-posting.
    > Q: What is the most annoying thing on usenet and in e-mail?


    disable DCOM via registry:
    http://support.microsoft.com/kb/825750

    i'll second the OpenOffice option.
     
    Salt_Peter, Dec 16, 2007
    #9
  10. George2

    James Kanze Guest

    On Dec 16, 7:11 am, "Alf P. Steinbach" <> wrote:
    > * yanlinlin:


    > > On 12月16æ—¥, 下åˆ1æ—¶49分, George2 <> wrote:
    > >> I am reading some code from other people, there are some code like
    > >> this,


    > >>
    Code:
    > >> class Foo {
    > >> };[/color][/color]
    >[color=green][color=darkred]
    > >> Foo& func()
    > >> {
    > >>         Foo foo;
    > >>         return foo;
    > >> }[/color][/color]
    >[color=green][color=darkred]
    > >> int main()
    > >> {
    > >>         Foo& foo = func();
    > >>         return 0;}
    > >> 


    > >> I want to confirm with you that it is not good code, since
    > >> we return a reference to local object instance, right? Even
    > >> if the code has expected function currently.


    > > I'm afraid this code can not be compiled. To return a
    > > reference of local non-static object is not allowed.


    > It's allowed and it should compile.


    > A compiler that doesn't allow it is non-conforming.


    > However, using that result incurs Undefined Behavior.


    I think that if the compiler can prouve that the result is used
    (and it is here), then it is allowed to reject the code. (The
    case here is an interesting one, in fact---I'm assuming that the
    same rules hold for references as for pointers, and that even
    copying an invalid reference is undefined behavior. I've not
    looked it up, however, so caveat empor.)

    > PS: Could you, or anyone, confirm whether you see this
    > posting? If you don't see it, simply do nothing.


    I've seen it. (Rather obviously.) I even figured out something
    on-topic to add so that I wouldn't be off topic in
    responding:).

    --
    James Kanze (GABI Software) email:
    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
     
    James Kanze, Dec 16, 2007
    #10
  11. * James Kanze:
    > On Dec 16, 7:11 am, "Alf P. Steinbach" <> wrote:
    >> * yanlinlin:

    >
    >>> On 12月16æ—¥, 下åˆ1æ—¶49分, George2 <> wrote:
    >>>> I am reading some code from other people, there are some code like
    >>>> this,

    >
    >>>>
    Code:
    >>>> class Foo {
    >>>> };
    >>>> Foo& func()
    >>>> {
    >>>>         Foo foo;
    >>>>         return foo;
    >>>> }
    >>>> int main()
    >>>> {
    >>>>         Foo& foo = func();
    >>>>         return 0;}
    >>>> 

    >
    >>>> I want to confirm with you that it is not good code, since
    >>>> we return a reference to local object instance, right? Even
    >>>> if the code has expected function currently.

    >
    >>> I'm afraid this code can not be compiled. To return a
    >>> reference of local non-static object is not allowed.

    >
    >> It's allowed and it should compile.

    >
    >> A compiler that doesn't allow it is non-conforming.

    >
    >> However, using that result incurs Undefined Behavior.

    >
    > I think that if the compiler can prouve that the result is used
    > (and it is here), then it is allowed to reject the code. (The
    > case here is an interesting one, in fact---I'm assuming that the
    > same rules hold for references as for pointers, and that even
    > copying an invalid reference is undefined behavior. I've not
    > looked it up, however, so caveat empor.)
    >
    >> PS: Could you, or anyone, confirm whether you see this
    >> posting? If you don't see it, simply do nothing.

    >
    > I've seen it. (Rather obviously.) I even figured out something
    > on-topic to add so that I wouldn't be off topic in
    > responding:).


    Thanks. I didn't think of reference initialization as "use". May have
    to adjust my internal C++ rule-set -- at least if someone cites the
    standard about this and it says so.

    Cheers,

    - Alf


    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, Dec 16, 2007
    #11
  12. George2

    yanlinlin Guest

    > It's allowed and it should compile.
    >
    > A compiler that doesn't allow it is non-conforming.
    >
    > However, using that result incurs Undefined Behavior.
    >
    > Cheers, & hth.,
    >
    > - Alf


    Yes, I misunderstood that.
    It can compile successfully, but with a warning. And the option "treat
    warning as error" misguided me about it. :p
     
    yanlinlin, Dec 16, 2007
    #12
  13. George2

    James Kanze Guest

    On Dec 16, 12:33 pm, "Alf P. Steinbach" <> wrote:

    > I didn't think of reference initialization as "use". May have
    > to adjust my internal C++ rule-set -- at least if someone cites the
    > standard about this and it says so.


    To be frank, I'm not sure that the standard says this
    explicitly. What I am fairly sure about is that there was an
    *intent* that references could be implemented as pointers, and
    that if you remap the reference operations in question as
    pointers, then you do have undefined behavior. So I think that
    the intent is that this be undefined behavior. (But I'm far
    from sure that the standard actually expresses this intent.
    Maybe a question in comp.std.c++ would be in order.)

    --
    James Kanze (GABI Software) email:
    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
     
    James Kanze, Dec 17, 2007
    #13
  14. George2

    Pavel Shved Guest

    On Dec 16, 13:39, James Kanze <> wrote:
    > On Dec 16, 7:11 am, "Alf P. Steinbach" <> wrote:
    >
    >
    >
    > > * yanlinlin:
    > > > On 12ÔÂ16ÈÕ, ÏÂÎç1ʱ49·Ö, George2 <> wrote:
    > > >> I am reading some code from other people, there are some code like
    > > >> this,
    > > >>
    Code:
    > > >> class Foo {
    > > >> };[/color][/color]
    >[color=green][color=darkred]
    > > >> Foo& func()
    > > >> {
    > > >>         Foo foo;
    > > >>         return foo;
    > > >> }[/color][/color]
    >[color=green][color=darkred]
    > > >> int main()
    > > >> {
    > > >>         Foo& foo = func();
    > > >>         return 0;}
    > > >> 
    > > >> I want to confirm with you that it is not good code, since
    > > >> we return a reference to local object instance, right? Even
    > > >> if the code has expected function currently.
    > > > I'm afraid this code can not be compiled. To return a
    > > > reference of local non-static object is not allowed.

    > > It's allowed and it should compile.
    > > A compiler that doesn't allow it is non-conforming.
    > > However, using that result incurs Undefined Behavior.

    >
    > I think that if the compiler can prouve that the result is used
    > (and it is here), then it is allowed to reject the code. (The
    > case here is an interesting one, in fact---I'm assuming that the
    > same rules hold for references as for pointers, and that even
    > copying an invalid reference is undefined behavior. I've not
    > looked it up, however, so caveat empor.)
    >



    How can compiler prove that the result is used? How can he prove i'm
    not writing a thread that's supposed to be externally terminated in
    the very same case as when the function happens to return a reference-
    to-local, the reference later explicitely being used but unreachable
    in practice? :)
     
    Pavel Shved, Dec 17, 2007
    #14
  15. On 16 Dec, 06:09, "Alf P. Steinbach" <> wrote:
    > * George2:
    >
    >
    >
    >
    >
    > > Hello everyone,

    >
    > > I am reading some code from other people, there are some code like
    > > this,

    >
    > >
    Code:
    > > class Foo {[/color]
    >[color=green]
    > > };[/color]
    >[color=green]
    > > Foo& func()
    > > {
    > >    Foo foo;
    > >    return foo;
    > > }[/color]
    >[color=green]
    > > int main()
    > > {
    > >    Foo& foo = func();[/color]
    >[color=green]
    > >    return 0;
    > > }
    > > 

    >
    > > I want to confirm with you that it is not good code, since we return a
    > > reference to local object instance, right? Even if the code has
    > > expected function currently.

    >
    > It's Undefined Behavior if the function result is ever used.
    >
    > Btw., could you or anyone else please confirm that you can see this posting.
    >
    > It seems that all internet connections out of Norway are down at the
    > moment, at least via my ISP (e.g. I can't reach Microsoft's servers, nor
    > Google), yet articles keep dropping in in international newsgroups.
    >
    > Of course, if you don't see this posting, simply don't respond.
    >


    i can c u. uk
     
    Nick Keighley, Dec 17, 2007
    #15
  16. George2

    James Kanze Guest

    On Dec 17, 11:56 am, Pavel Shved <> wrote:
    > On Dec 16, 13:39, James Kanze <> wrote:


    > > On Dec 16, 7:11 am, "Alf P. Steinbach" <> wrote:


    > > > * yanlinlin:
    > > > > On 12月16æ—¥, 下åˆ1æ—¶49分, George2 <> wrote:
    > > > >> I am reading some code from other people, there are some code like
    > > > >> this,
    > > > >>
    Code:
    > > > >> class Foo {
    > > > >> };[/color][/color][/color]
    [color=blue][color=green][color=darkred]
    > > > >> Foo& func()
    > > > >> {
    > > > >>         Foo foo;
    > > > >>         return foo;
    > > > >> }[/color][/color][/color]
    [color=blue][color=green][color=darkred]
    > > > >> int main()
    > > > >> {
    > > > >>         Foo& foo = func();
    > > > >>         return 0;}
    > > > >> 
    > > > >> I want to confirm with you that it is not good code, since
    > > > >> we return a reference to local object instance, right? Even
    > > > >> if the code has expected function currently.
    > > > > I'm afraid this code can not be compiled. To return a
    > > > > reference of local non-static object is not allowed.
    > > > It's allowed and it should compile.
    > > > A compiler that doesn't allow it is non-conforming.
    > > > However, using that result incurs Undefined Behavior.


    > > I think that if the compiler can prouve that the result is
    > > used (and it is here), then it is allowed to reject the
    > > code. (The case here is an interesting one, in fact---I'm
    > > assuming that the same rules hold for references as for
    > > pointers, and that even copying an invalid reference is
    > > undefined behavior. I've not looked it up, however, so
    > > caveat empor.)


    > How can compiler prove that the result is used?


    By flow analysis.

    > How can he prove i'm not writing a thread that's supposed to
    > be externally terminated in the very same case as when the
    > function happens to return a reference- to-local, the
    > reference later explicitely being used but unreachable in
    > practice? :)


    That's undefined behavior as well, as far as the standard goes,
    so the compiler doesn't have to take it into account. (In fact,
    of course, it's also impossible to do on most modern systems;
    another process can't really look at the individual instructions
    you're executing.)

    --
    James Kanze (GABI Software) email:
    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
     
    James Kanze, Dec 17, 2007
    #16
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Patrick Hoffmann
    Replies:
    3
    Views:
    2,854
    Christian Janßen
    Aug 8, 2003
  2. Kench
    Replies:
    2
    Views:
    5,423
    Victor Bazarov
    Jun 28, 2004
  3. romerun
    Replies:
    6
    Views:
    15,077
    Alf P. Steinbach
    Sep 3, 2004
  4. George2
    Replies:
    5
    Views:
    561
    James Kanze
    Jan 25, 2008
  5. Replies:
    2
    Views:
    491
    Jim Langston
    May 30, 2008
Loading...

Share This Page