returning STL objects

This is a discussion on returning STL objects within the C++ Programming forums, part of the General Programming Boards category; Code: vector<int> test() { vector<int> eg; [.......] return eg; } vector<int> myvec = test(); Is this copied , or are ...

  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
    21,660
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,506
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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, 11:39 AM
  5. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21