dangling reference

Discussion in 'C++' started by Hans Van den Eynden, Oct 14, 2004.

  1. I thought that integers resized on the stack and that a value type was
    just defined in the scope of the function. And when the function
    returns the local variables were removed from the stack. But what is
    the explication of this code??

    int& dangling_reference();

    int main(int argc, char *argv[])
    {
    int a;
    a= dangling_reference();
    cout<<a<<endl;
    cin.get();
    }



    int& dangling_reference() {
    int x=3;
    return x;
    }
    Hans Van den Eynden, Oct 14, 2004
    #1
    1. Advertising

  2. "Hans Van den Eynden" <> wrote in message
    news:...
    >I thought that integers resized on the stack and that a value type was
    > just defined in the scope of the function. And when the function
    > returns the local variables were removed from the stack. But what is
    > the explication of this code??
    >
    > int& dangling_reference();
    >
    > int main(int argc, char *argv[])
    > {
    > int a;
    > a= dangling_reference();
    > cout<<a<<endl;
    > cin.get();
    > }
    >
    >
    >
    > int& dangling_reference() {
    > int x=3;
    > return x;
    > }


    That code has undefined behaviour, because you are using a reference to an
    object that has been destroyed. What were you expecting? What do you see?

    john
    John Harrison, Oct 14, 2004
    #2
    1. Advertising

  3. Hans Van den Eynden

    Method Man Guest

    "Hans Van den Eynden" <> wrote in message
    news:...
    > I thought that integers resized on the stack and that a value type was
    > just defined in the scope of the function. And when the function
    > returns the local variables were removed from the stack. But what is
    > the explication of this code??
    >
    > int& dangling_reference();
    >
    > int main(int argc, char *argv[])
    > {
    > int a;
    > a= dangling_reference();
    > cout<<a<<endl;
    > cin.get();
    > }
    >
    >
    >
    > int& dangling_reference() {
    > int x=3;
    > return x;
    > }


    This is UB. If you are getting the correct result (printing "3"), then
    you're just lucky. This probably occured because your program is small and
    the contents of the memory address of 'x' are still intact. Regardless, you
    should not *expect* your code to work.
    Method Man, Oct 14, 2004
    #3
  4. Method Man wrote:
    >

    [snip]
    >
    > This is UB. If you are getting the correct result (printing "3"), then
    > you're just lucky.


    :)

    Some would say: He was unlucky.
    The reason: If he were lucky, the program would have crashed immediatly
    indicating that there is a problem somewhere. So he is unlucky in that there
    is a problem which was not found during testing and waits as a time bomb for
    the moment the end user first tries that program (and according to Murphy
    crashes immediatly)

    --
    Karl Heinz Buchegger
    Karl Heinz Buchegger, Oct 15, 2004
    #4
  5. Hans Van den Eynden

    JKop Guest

    Hans Van den Eynden posted:

    > I thought that integers resized on the stack and that a value type was
    > just defined in the scope of the function. And when the function
    > returns the local variables were removed from the stack. But what is
    > the explication of this code??
    >
    > int& dangling_reference();
    >
    > int main(int argc, char *argv[])
    > {
    > int a;
    > a= dangling_reference();
    > cout<<a<<endl;
    > cin.get();
    > }
    >
    >
    >
    > int& dangling_reference() {
    > int x=3;
    > return x;
    > }



    The following would be valid:

    int& Blah()
    {
    int &x = *new int(3);

    return x;
    }


    But then ofcourse the calling function would have to call "delete".

    In circumstances like this, think to yourself, "Could I use a pointer
    here?". If the answer's no, then you can't use a reference either. For
    instance, the following may be more obviously invalid to you:

    int* Blah()
    {
    int x = 7;

    return &x;
    }


    A lot of compiler's warn you when you do the above.


    Warning: line 5: Returning reference to local object


    -JKop
    JKop, Oct 15, 2004
    #5
  6. Hans Van den Eynden

    JKop Guest


    > A lot of compiler's warn you when you do the above.



    No, I'm not illiterate, that was a typo! "compilers".


    -JKop
    JKop, Oct 15, 2004
    #6
    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. rootz anabo
    Replies:
    0
    Views:
    453
    rootz anabo
    Feb 3, 2005
  2. Hans Van den Eynden

    dangling reference

    Hans Van den Eynden, Oct 16, 2004, in forum: Java
    Replies:
    1
    Views:
    2,848
    Joona I Palaste
    Oct 16, 2004
  3. kaede

    Dangling Reference

    kaede, Oct 27, 2003, in forum: C++
    Replies:
    4
    Views:
    415
    lilburne
    Oct 28, 2003
  4. Nick Keighley

    dangling reference

    Nick Keighley, Apr 3, 2005, in forum: C++
    Replies:
    1
    Views:
    333
    Marcin Kalicinski
    Apr 3, 2005
  5. Belebele

    Dangling Reference. Idiom

    Belebele, Feb 7, 2007, in forum: C++
    Replies:
    10
    Views:
    545
    Grizlyk
    Feb 8, 2007
Loading...

Share This Page