Thread: returning STL objects

  1. #1
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300

    returning STL objects

    Code:
    vector<int> test() {
    	vector<int> eg;
            [.......]
    	return eg;
    }    
    vector<int> myvec = test();
    Is this copied, or are references implicitly used?

    In other words, if eg turns out to have 5000 elements and I call test in a loop a few thousand times, AND eg is created and then copied each time -- this is not so good.

    I was hoping the magic of ref counting would mean that I could return a reference to eg, but I guess eg is still just a local variable, so it seems the only way around this is:

    Code:
    vector<int> *test() {
    	vector<int> *eg = new vector<int>();
    	return eg;
    }
    Or have I misunderstood something?
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by MK27
    Is this copied, or are references implicitly used?
    The vector may or may not be copied, depending on whether named return value optimisation is performed by the compiler. That is, the compiler might be able to elide the copying if it can recognise that NRVO may be applied.

    Neither references nor reference counting is involved here. Generally, if you do not want to rely on return value optimisation, you would just provide the function with a reference parameter.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by laserlight View Post
    Generally, if you do not want to rely on return value optimisation, you would just provide the function with a reference parameter.
    Sounds good.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well semantically, there is a copy of the whole vector going on.

    But modern compilers should be doing this
    Return value optimization - Wikipedia, the free encyclopedia

    Edit:
    0/2 vs. laserlight today
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    If you do it in a tight loop, it might be better performance-wise to move the creation of the vector out of the loop:

    Code:
    void test(vector<int>&);
    
    int main()
    {
        vector<int> vec;
        while (sth) {
            vec.clear(); //or test might clear it
            test(vec);
            ...
        }
    }
    Whatever you do, don't allocate the vector with new. It ain't that good performance-wise either, and it defeats the purpose of a vector as an automatically managed array (you'll need to make sure it is deleted).
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Objects or pointers in STL containers?
    By zacs7 in forum C++ Programming
    Replies: 7
    Last Post: 06-23-2009, 10:25 AM
  2. Returning an Array of Pointers to Objects
    By randomalias in forum C++ Programming
    Replies: 4
    Last Post: 04-29-2006, 02:45 PM
  3. dynamic memory alloccation & returning objects
    By haditya in forum C++ Programming
    Replies: 8
    Last Post: 04-21-2005, 11:55 PM
  4. Returning reference to STL String class
    By frisbee in forum C++ Programming
    Replies: 5
    Last Post: 12-29-2003, 12:39 PM
  5. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM