return type of a function that returns a local variable

Discussion in 'C++' started by Jess, Mar 22, 2007.

  1. Jess

    Jess Guest

    Hello,

    I understand that if a function "f" has a local variable "a", and
    after it returns, "a" vanishes. If "f" returns "a" as a result, then I
    noticed the following:

    1. if the return type is "a&", then compiler complains reference to
    the local variable "a".
    2. if the return type is "a", then everything works fine.

    I think this is because in the first case, the return is copy-by-
    reference, and we can't reference a local var. In the second case, it
    is a copy-by-value, and it's correct because we can copy the value
    from a local var, is this right?

    Thanks!
    Jess, Mar 22, 2007
    #1
    1. Advertising

  2. Jess

    Gavin Deane Guest

    On 22 Mar, 12:11, "Jess" <> wrote:
    > Hello,
    >
    > I understand that if a function "f" has a local variable "a", and
    > after it returns, "a" vanishes. If "f" returns "a" as a result, then I
    > noticed the following:
    >
    > 1. if the return type is "a&", then compiler complains reference to
    > the local variable "a".
    > 2. if the return type is "a", then everything works fine.


    Your description is a bit confused. In your first paragraph a is an
    object, in your second a is a type. But I think I understand what you
    mean.

    > I think this is because in the first case, the return is copy-by-
    > reference, and we can't reference a local var. In the second case, it
    > is a copy-by-value, and it's correct because we can copy the value
    > from a local var, is this right?


    Yes.

    // foo1 is bad. Don't do this.
    int& foo1()
    {
    int i = 42;
    return i;
    }

    // foo2 is OK.
    int foo2()
    {
    int j = 0;
    return j;
    }

    int main()
    {
    int& int_reference = foo1();
    int int_value = foo2();
    }

    In the code above, in the function main, int_reference is initialised
    as a *reference to* the int i inside function foo1. As you correctly
    suspect, after foo1 returns i no longer exists and so int_reference
    refers to nothing. It is a dangling reference. Any use of
    int_reference has undefined behaviour.

    int_value on the other hand is initialised with *a copy* of the int j
    from inside function foo2. It doesn't matter that j no longer exists
    after foo2 returns because the copy of j was taken while j did still
    exist.

    Gavin Deane
    Gavin Deane, Mar 22, 2007
    #2
    1. Advertising

  3. Jess

    Jess Guest

    Thanks, that solves my problem! :)
    Jess, Mar 22, 2007
    #3
    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. octavio
    Replies:
    4
    Views:
    779
    Christopher Benson-Manica
    Dec 15, 2005
  2. Diego Martins
    Replies:
    5
    Views:
    476
    Diego Martins
    Sep 5, 2006
  3. sairam
    Replies:
    2
    Views:
    357
    Steve Holden
    Apr 5, 2007
  4. hari
    Replies:
    13
    Views:
    529
    David Thompson
    Dec 2, 2007
  5. Mark
    Replies:
    7
    Views:
    1,301
    Keith Thompson
    Feb 4, 2011
Loading...

Share This Page