Thread: Returning a pointer to a vector

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    36

    Returning a pointer to a vector

    Hello,

    I have a class which contains a vector. This vector is created in a function called "CreateVector". So far, I have created this vector in this function, and returned the vector as a whole. However, because the vector is large, ideally I'd like to be able to return a pointer to the vector. How can I do this? Can you create a pointer to a vector?

    Here is what I have so far:

    Code:
    #include <vector>
    
    using namespace std;
    
    class My_Class
    {
    private:
        vector<int*> x;
        vector<int*> CreateVector()
        {
            vector<int*> y(100);
            for (int i = 0; i < 100; i ++)
            {
                y[i] = i * i;
            }
            return y;
        };
    public:
        void SetX()
        {
            x = CreateVector();
        };
    };
    
    in main()
    {
        My_Class my_object;
        my_object.SetX();
    
        return 0;
    }
    (This is just an example piece of code to demonstrate my problem, the actual CreateVector() function does much more than is shown)

    What do I need to do so that I can return a pointer to this vector, in order to speed up the program?

    Thanks!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You would need to use dynamic memory allocation to return a pointer to a vector whose lifetime exceeds that of the function call. (Or use a static local variable, but that is a bad solution here.)

    I suggest that you change the function signature to add a reference parameter to the vector. This way the call will pass an empty vector that the function will fill.

    That said, a compiler optimisation known as return value optimisation can make it such that there is no extra cost of copying in returning the vector by value... but then this optimisation is not always applied.
    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
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    But they also say: don't optimize prematurely.
    Return the vector by value and if you find that it is running slowly, then you can change the design.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Jan 2007
    Posts
    330
    Code:
    vector<int*> y(100);
    for (int i = 0; i < 100; i ++)
    {
      y[i] = i * i;
    }
    return y;
    y is a vector of int pointers so doing y[i] = i*i; is wrong. You need vector<int>

    As for your question, in the current code it looks to me that you can just operate on x. Both the function and x are prive anyway.

    Code:
    x.resize(100);
    for()
    x[i] = i*i;
    x should also be vector<int> instead of vector<int *> of course

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Delete and vector pointer
    By 6tr6tr in forum C++ Programming
    Replies: 18
    Last Post: 04-15-2008, 11:03 AM
  3. syntax help?
    By scoobygoo in forum C++ Programming
    Replies: 1
    Last Post: 08-07-2007, 10:38 AM
  4. Vector class
    By Desolation in forum C++ Programming
    Replies: 2
    Last Post: 05-12-2007, 05:44 PM
  5. I want to pass in a pointer to a vector of type Triangle*
    By Shadow12345 in forum C++ Programming
    Replies: 1
    Last Post: 10-13-2002, 08:26 AM