none
Returning a STL vector from a function…

    Question

  • Hi!

     

    In below code snippet I populate a vector with a couple of values. Everything works okay as long as I don’t leave the function. The minute I go outside, the vector gets loaded with rubbish from memory.

     

    When I run the code and checks the vector in the Watch window it looks okay as long as I stay in the function. When I get outside it gets real scary, why?

     

    Below have been simplified.

     

    Code Snippet

    std::vector<CString> get_lstAlpha_Y1()

    {

     

      std::vector<CString> lstAlpha_Y1( 0 );

     

      while( pFeaturePlacement )

      {

        if( lstWhatEver.count() > 0 )

        {

          lst_Y1.push_back(pFeaturePlacement->get_strAngleDegrees() ) );

        }

      pFeaturePlacement = pFeaturePlacement->get_PreviousPlacement();

      }

      return lst_Y1;

    }

     

     

    Inside the function the Watch window give me:

     

    Code Snippet

    + lst_Y1         [2]("65","105")

     

     

    Outside the function the Watch window give me:

     

    Code Snippet
    get_lstAlpha_Y1()                [500780]("65","105",<Bad Ptr>,<Bad Ptr>,<Bad Ptr>,<Bad Ptr>,<Bad Ptr>,<Bad Ptr>,<Bad Ptr>,"f_uv","øìá   €ïá                 "," A•èA•",<Bad ………….and it keeps on….

     

    I know that returning the “entire thing” might not be so clever. Instead I could p*** the vector as a reference or as a as pointer but I had hoped I could use above method.

     

    By the way when I try "get_lstAlpha_Y1().size()" I get the right number of items, not [500780] .

     

    Regards

    KC

    Wednesday, March 12, 2008 10:30 AM

Answers

  • Well...the question is which vector shows "garbage"? The local one ('lst_Y1')? This is normal since once you return from the function, all local variables gets destroyed and cleaned up. The values stored in the local one have been copied to the returned vector though. In other words:

    std::vector<CString> rc = get_lstAlpha_Y1();

    'rc' should contain all returned values and should not show "garbage" inside the debugger.

    Finally...as you have pointed out....returning a copy of the vector in the first place is not the best idea in terms of performance...depending on the number of items...
    Wednesday, March 12, 2008 10:55 AM

All replies

  • Well...the question is which vector shows "garbage"? The local one ('lst_Y1')? This is normal since once you return from the function, all local variables gets destroyed and cleaned up. The values stored in the local one have been copied to the returned vector though. In other words:

    std::vector<CString> rc = get_lstAlpha_Y1();

    'rc' should contain all returned values and should not show "garbage" inside the debugger.

    Finally...as you have pointed out....returning a copy of the vector in the first place is not the best idea in terms of performance...depending on the number of items...
    Wednesday, March 12, 2008 10:55 AM
  • Hi!

     

    I forgot to say thank you 

     

    I have passed the vector as a reference, no problems. I was more interested in the issue from a philosophical point of view. The fact that one is allowed to return a vector that gets a totally random size. I just find it a bit odd that there’s no warning from the compiler.

     

    Regards

    KC

     

    Wednesday, March 12, 2008 2:25 PM
  •  Kennet wrote:

    I have passed the vector as a reference, no problems. I was more interested in the issue from a philosophical point of view. The fact that one is allowed to return a vector that gets a totally random size. I just find it a bit odd that there’s no warning from the compiler.



    That is pretty much the whole purpose of the vector and any other class following the RAII (Resource Acquisition Is Initialization) idiom. The vector is a self-contained resource and thus is certainly being able to allow things like copying, returning etc. without the use of helper functions (standard arrays require 'memcpy()' etc. for doing the same thing or are even limited in some functionality).


    And that is also the reason where there is no need to issue a warning. Since a vector management its content ibn every single regard (memory management, copying, construction, destruction etc.), it is a safe operation to e.g. returning a vector from a function -disregarding the number of items.

    Wednesday, March 12, 2008 3:38 PM