return address of temporary variable?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • thomas

    return address of temporary variable?

    -----code----
    vector<int&calc (){
    vector<inttemp;
    temp.push_back( 3);

    return temp;
    }
    ----code----

    "temp" is a temporary variable, when returned, it's like return by
    alias.
    So is the code above legal?
  • Abdullah

    #2
    Re: return address of temporary variable?

    On Mar 9, 7:14 pm, thomas <FreshTho...@gm ail.comwrote:
    -----code----
    vector<int&calc (){
    vector<inttemp;
    temp.push_back( 3);
    >
    return temp;}
    >
    ----code----
    >
    "temp" is a temporary variable, when returned, it's like return by
    alias.
    So is the code above legal?
    No, since temp will be destroyed when the function returns. The
    returned reference will be a dangling one.

    Comment

    • thomas

      #3
      Re: return address of temporary variable?

      well, thanks.
      but if I put the "temp" vector out of the function, it seems a little
      ugly.
      so I want it to be clean, and i want to "new" a vector in the function
      scope, and return the "*temp".
      but it seems that i will have to delete it somewhere.

      anyway, it seems not very neat. So is my feeling just wrong? or any
      other way to write it to make me feel better?

      Comment

      • Juha Nieminen

        #4
        Re: return address of temporary variable?

        thomas wrote:
        -----code----
        vector<int&calc (){
        vector<inttemp;
        temp.push_back( 3);
        >
        return temp;
        }
        ----code----
        >
        "temp" is a temporary variable, when returned, it's like return by
        alias.
        So is the code above legal?
        If you mean "legal" as in "compiles", then the answer is yes. If you
        mean "legal" as in "works", the answer is no.

        Returning a reference to a temporary is basically no different than
        returning a pointer to a temporary. In either case if you try to access
        the pointed object, bad things will happen.

        To do what you want you have several options. To do *exactly* what you
        are wanting to do above, either return by value (instead of by
        reference), or return a dynamically allocated instance (preferably using
        a smart pointer or std::auto_ptr).
        In the larger context other solutions may be better.

        Comment

        • =?UTF-8?B?RXJpayBXaWtzdHLDtm0=?=

          #5
          Re: return address of temporary variable?

          On 2008-03-09 15:36, thomas wrote:
          well, thanks.
          but if I put the "temp" vector out of the function, it seems a little
          ugly.
          so I want it to be clean, and i want to "new" a vector in the function
          scope, and return the "*temp".
          but it seems that i will have to delete it somewhere.
          >
          anyway, it seems not very neat. So is my feeling just wrong? or any
          other way to write it to make me feel better?
          As Juhu Nieminen pointed out, returning a copy might be a good idea:

          vector<intcalc( ){
          // ^^^ Notice the lack of &
          vector<inttemp;
          temp.push_back( 3);

          return temp;
          }

          --
          Erik Wikström

          Comment

          Working...