   Speed and the STL vector...   SOLVED
  It struck me that with any array you can simply return a pointer, like this...char* foo(){   char* bar = new char[100000];   return bar;}...but what if you have a vector? You don't want to copy large vectors every time you step out of functions...std::vector<char> foo(){   std::vector<char> bar;   bar.resize(100000);   return bar;}So what i'm wondering is if there's a similar way as in code-1 for vectors? Will the vector still go out of scope if you return a reference to a locally created vector? ...and creating a vector with the new operator seems rather ridiculous. I figure you can use a vector reference as parameter to a fuction, but that is not what i'm asking for, it's if it's possible to speed up the returning i need... [smile]Thanks for any inputs![Edited by - Android_s on January 20, 2005 9:13:50 AM]
  If you do it that way, a second copy of bar will be made when it is returned and assigned to whichever vector object you are assigning it to.Have you tried making the return type a reference or pointer to the vector? ie: vector &func(void); or vector *func(void);Seems like passing in a reference by parameter is a good way to do what you're wanting to achieve.   ; ...and creating a vector with the new operator seems rather ridiculous. why?;  void foo(std::vector<char> &v) {    v.resize(10000);}No performance penalty due to copying. I'm also positive that any reasonably optimising compiler would be able to avoid the temporary object for a function like this:std::vector<char> foo() {    return std::vector<char>(10000);};Quote:Original post by Android_sI figure you can use a vector reference as parameter to a fuction, but that is not what i'm asking for, it's if it's possible to speed up the returning i need...Check out Andrei Alexandrescu's MOJO library (see e.g. here)Quote:Original post by darookieI'm also positive that any reasonably optimising compiler would be able to avoid the temporary object for a function like this: *snip*Yes, that's RVO.   ;Quote:Original post by Android_sSo what i'm wondering is if there's a similar way as in code-1 for vectors?There are some ways, they involve the idea is moving away from copy semantics to *move* semantics, i'll explain at the end.Quote:Original post by Android_sWill the vector still go out of scope if you return a reference to a locally created vector?of-course.Quote:Original post by Android_s...and creating a vector with the new operator seems rather ridiculous.not really only when compared to just passing a vector by reference to a function.Quote:Original post by Android_sit's if it's possible to speed up the returning i need... [smile]Well first there is the possibility of your compiler doing RVO == return value optimization.Next your compiler may have support for move sematics (only a thew do), if yours don't then there is a way to get move semantics support in library form, one such library is known as Mojo invented by Andrei Alexandrescu, it's based on the idea of writing *move* constructors as oposed to or including copy constructors you can read more about this here.Andrei Alexandrescu also has written a custom std::vector (yasli::vector) which implements move semantics and you get can the source code from his site.A group of people are pressing support for move semantics in the c++ standard but its not currently in the standard.   ;  I made some speed tests comparing the time for returning a whole vector and passing it as a param, and clearly the latter is much faster.Well, i think it's more neat looking when returning something from a function so i tried returning a reference and pointer, but it seems the local vector is indeed going out of scope...Quote: ...and creating a vector with the new operator seems rather ridiculous. why?It might just be a personal opinion, but it feels that the point with using the vector is somewhat destroyed when fiddeling with that new vector on it...And what happens when you destroy the pointer, is the vector's allocated space also removed..?;  If you want to make a function that allocates vector, but takes no ownershipstd::vector<char>* foo(){    return new std::vector<char>(100000);}when you delete the pointer, it destroys everything the vector contains.I'd still prefer pass by reference..   ;Quote:Original post by Android_sAnd what happens when you destroy the pointer, is the vector's allocated space also removed..?Yes, all the standard library containers have properly written destructors, if they didn't you would be leaking memory all over the place with-out even calling new once!.As soon as you call the delete operator there destructors are invoked which will release there own resources before the actual container itself is deleted.Anyways like me & Fruny mentioned if you really must need this you can look up about move semantics & use the Mojo framework (or Andrei Alexandrescu's custum vector).   ;  So, it seems like passing by reference is the most safe/fast/easy way to do it for now, but i went and downloaded that YASLI vector, thought i should try it out....and nah, i don't have to use returning, just exploring the possibilities.Thanks! [smile]   