return reference to local variable ?

Discussion in 'C++' started by romerun, Sep 2, 2004.

  1. romerun

    romerun Guest

    Hello,

    This make me very confusing:

    ----------------------------------
    int &kkk() {
    int a = 5;
    return a;
    }

    int main() {
    int b = kkk();
    }
    ----------------------------------

    "b" should be undefined as many articles say. However, I have tested
    in g++ and Visual C++, the result of b is 5 !?

    I guess g++ and Visual C++ choose to implement the "undefined" state
    in this case as trying to get the value of the reference to local
    variable thereby making "b" has the expected result

    I'm I wrong ?
     
    romerun, Sep 2, 2004
    #1
    1. Advertising

  2. * romerun:
    > Hello,
    >
    > This make me very confusing:
    >
    > ----------------------------------
    > int &kkk() {
    > int a = 5;
    > return a;
    > }
    >
    > int main() {
    > int b = kkk();
    > }
    > ----------------------------------
    >
    > "b" should be undefined as many articles say. However, I have tested
    > in g++ and Visual C++, the result of b is 5 !?
    >
    > I guess g++ and Visual C++ choose to implement the "undefined" state
    > in this case as trying to get the value of the reference to local
    > variable thereby making "b" has the expected result
    >
    > I'm I wrong ?


    Yes.

    It's simply that nothing has yet corrupted the memory area that the local
    variable occupied, at the point where the value is used.

    There is no guarantee for that and it might turn out differently with
    different compiler options (not to mention different compilers).

    --
    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, Sep 2, 2004
    #2
    1. Advertising

  3. romerun wrote:
    >
    > Hello,
    >
    > This make me very confusing:
    >
    > ----------------------------------
    > int &kkk() {
    > int a = 5;
    > return a;
    > }
    >
    > int main() {
    > int b = kkk();
    > }
    > ----------------------------------
    >
    > "b" should be undefined as many articles say. However, I have tested
    > in g++ and Visual C++, the result of b is 5 !?
    >
    > I guess g++ and Visual C++ choose to implement the "undefined" state
    > in this case as trying to get the value of the reference to local
    > variable thereby making "b" has the expected result
    >
    > I'm I wrong ?


    Yep.
    The above works just per coincidence.
    In this particular program the memory state of the
    program hasn't changed enough and thus you get the
    result you expect. But as said: This is just a
    coincidence in this special case. It is *not*
    because the compiler writers tryed to help
    you in some clever way.

    When a variable goes out of scope, we say the variable
    is destroyed. Well. That's what we say. But in a computer
    acutally nothing is physically destroyed. The variable
    was assigned a memory location and that memory location
    holds the value. So when a variable gets destroyed, its
    corresponding memory location is marked as beeing free
    but that's it. The memory location still holds the value,
    until the program uses it for something else.

    --
    Karl Heinz Buchegger
     
    Karl Heinz Buchegger, Sep 2, 2004
    #3
  4. romerun wrote:
    > int &kkk() { int a = 5; return a; }
    > int main() { int b = kkk(); }
    >
    > "b" should be undefined as many articles say. However, I have tested
    > in g++ and Visual C++, the result of b is 5 !?


    In this particular case it's 5 only because that particular location has
    been freed but not yet reused. Not only is this specific to this
    program, compiler, and set of compiler options, but if you hold onto the
    reference which is returned, insert a call to another function, and then
    check the value of b, you'll see how fragile this behaviour can be:

    #include <iostream>
    int &kkk() { int a = 5; return a; }
    void jjj() { int d = 10; }
    int main() {
    int& b = kkk();
    jjj();
    std::cout << b;
    }

    Although the output of this program is undefined, on your platform with
    optimizations off it's likely to output 10.
    --
    Derrick Coetzee
    I grant this newsgroup posting into the public domain. I disclaim all
    express or implied warranty and all liability. I am not a professional.
     
    Derrick Coetzee, Sep 2, 2004
    #4
  5. romerun

    romerun Guest

    Karl Heinz Buchegger <> wrote in message news:<>...
    > romerun wrote:
    > >
    > > Hello,
    > >
    > > This make me very confusing:
    > >
    > > ----------------------------------
    > > int &kkk() {
    > > int a = 5;
    > > return a;
    > > }
    > >
    > > int main() {
    > > int b = kkk();
    > > }
    > > ----------------------------------
    > >
    > > "b" should be undefined as many articles say. However, I have tested
    > > in g++ and Visual C++, the result of b is 5 !?
    > >
    > > I guess g++ and Visual C++ choose to implement the "undefined" state
    > > in this case as trying to get the value of the reference to local
    > > variable thereby making "b" has the expected result
    > >
    > > I'm I wrong ?

    >
    > Yep.
    > The above works just per coincidence.
    > In this particular program the memory state of the
    > program hasn't changed enough and thus you get the
    > result you expect. But as said: This is just a
    > coincidence in this special case. It is *not*
    > because the compiler writers tryed to help
    > you in some clever way.
    >
    > When a variable goes out of scope, we say the variable
    > is destroyed. Well. That's what we say. But in a computer
    > acutally nothing is physically destroyed. The variable
    > was assigned a memory location and that memory location
    > holds the value. So when a variable gets destroyed, its
    > corresponding memory location is marked as beeing free
    > but that's it. The memory location still holds the value,
    > until the program uses it for something else.


    Do you have cases of "return ref to local var" that doesn't work (in g++ or vc++) ?
     
    romerun, Sep 3, 2004
    #5
  6. romerun wrote:

    > Karl Heinz Buchegger <> wrote in message news:<>...
    >
    >>romerun wrote:
    >>
    >>>Hello,
    >>>
    >>> This make me very confusing:
    >>>
    >>>----------------------------------
    >>>int &kkk() {
    >>> int a = 5;
    >>> return a;
    >>>}
    >>>
    >>>int main() {
    >>> int b = kkk();
    >>>}
    >>>----------------------------------
    >>>
    >>>"b" should be undefined as many articles say. However, I have tested
    >>>in g++ and Visual C++, the result of b is 5 !?
    >>>
    >>>I guess g++ and Visual C++ choose to implement the "undefined" state
    >>>in this case as trying to get the value of the reference to local
    >>>variable thereby making "b" has the expected result
    >>>
    >>>I'm I wrong ?

    >>
    >>Yep.
    >>The above works just per coincidence.
    >>In this particular program the memory state of the
    >>program hasn't changed enough and thus you get the
    >>result you expect. But as said: This is just a
    >>coincidence in this special case. It is *not*
    >>because the compiler writers tryed to help
    >>you in some clever way.
    >>
    >>When a variable goes out of scope, we say the variable
    >>is destroyed. Well. That's what we say. But in a computer
    >>acutally nothing is physically destroyed. The variable
    >>was assigned a memory location and that memory location
    >>holds the value. So when a variable gets destroyed, its
    >>corresponding memory location is marked as beeing free
    >>but that's it. The memory location still holds the value,
    >>until the program uses it for something else.

    >
    >
    > Do you have cases of "return ref to local var" that doesn't work (in g++ or vc++) ?


    If you tried this in an environment that allowed multiple threads of
    execution, it is guaranteed to fail. Usually at a bad time.

    -Rich
    --
    Richard Pennington
    Email:
    http://www.pennware.com ftp://ftp.pennware.com
     
    Richard Pennington, Sep 3, 2004
    #6
  7. * romerun:
    >
    > Do you have cases of "return ref to local var" that doesn't work (in g++ or vc++) ?


    To corrupt the memory formerly occupied by the variable, simply evaluate
    an (suitable) expression involving variables that cannot be optimized
    away, or pass the reference to a function that has its own locals.

    --
    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, Sep 3, 2004
    #7
    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,850
    Christian Jan├čen
    Aug 8, 2003
  2. Kench
    Replies:
    2
    Views:
    5,420
    Victor Bazarov
    Jun 28, 2004
  3. George2
    Replies:
    15
    Views:
    704
    James Kanze
    Dec 17, 2007
  4. George2
    Replies:
    5
    Views:
    559
    James Kanze
    Jan 25, 2008
  5. Replies:
    2
    Views:
    491
    Jim Langston
    May 30, 2008
Loading...

Share This Page