vector of pointers and assignment within function

This is a discussion on vector of pointers and assignment within function within the C++ Programming forums, part of the General Programming Boards category; I'm having trouble using a function call to assign values to an empty vector of pointers. I can use a ...

  1. #1
    Registered User
    Join Date
    Jun 2008
    Location
    Toronto, Ontario, Canada
    Posts
    17

    vector of pointers and assignment within function

    I'm having trouble using a function call to assign values to an empty vector of pointers. I can use a function to assign values to an ordinary array of pointers, but when I try to do the same using a vector, the assignment fails.

    In the following code, the call to the vector function assigns values within the function scope, but outside the scope (i.e. in "main") the vector remains empty.

    The output of this code is:
    Size in function: 5
    Size in main: 0

    What am I missing?

    Code:
    #include <iostream>
    #include <vector>
    
    using namespace std;
    
    void fillvector(vector<int*> v);
    
    int main()
    {
            int i;
    	vector<int*> intvector;
    
    	//try to fill vector with values
    	fillvector(intvector);
     
            //check the result
    	cout 	<< "\nSize in main: " << intvector.size() << flush;
    
    	return 0;
    }
    
    void fillvector(vector<int*> v) 
    {
    	int i;
    	int* ptr;
    
    	for(i = 0; i < 5; i++) {
    		ptr = new int(i+1);
    		v.push_back(ptr);
    	}
    	cout << "\nSize in function: " << v.size() << flush;
    }

  2. #2
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    You're passing the vector by value, which just makes a local copy of it.
    Pass it by reference instead.

  3. #3
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,752
    You passed the container by value: The vector had made a copy of itself and the function filled that copy. It would be your responsibility to assign the "new" container:

    I would do it like this:

    std::vector<int> v;
    v = fillvector( v );

  4. #4
    Registered User
    Join Date
    Jun 2008
    Location
    Toronto, Ontario, Canada
    Posts
    17
    Do you mean, the function should be like this?

    Code:
    void fillvec(vector<int*>& v)

  5. #5
    Registered User
    Join Date
    Jun 2008
    Location
    Toronto, Ontario, Canada
    Posts
    17
    Thanks for you help!

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    BTW, why are you using dynamic memory here? In this example a vector<int> would be simpler. In a more complex example the memory leaks that you have now could cause problems for your program.

  7. #7
    Registered User
    Join Date
    Jun 2008
    Location
    Toronto, Ontario, Canada
    Posts
    17
    The vector (which will actually hold class objects, not ints as in my example) must hold a large data set, which is then processed to extract a much smaller subset. Once the subset has been extracted the larger set can then deleted to save memory.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,159
    The vector (which will actually hold class objects, not ints as in my example) must hold a large data set, which is then processed to extract a much smaller subset. Once the subset has been extracted the larger set can then deleted to save memory.
    Perhaps you would be better off creating the large vector of objects (not pointers) in a function, do the processing in that function, and then return (perhaps via an out parameter) the smaller subset result, leaving the temporary large data set to be destroyed automatically as control leaves the scope of the function. That way you take advantage of the memory management offered by the vector. Alternatively, you could use a vector of shared_ptrs, or look to Boost for a boost with ptr_vector.
    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

  9. #9
    Registered User
    Join Date
    Jun 2008
    Location
    Toronto, Ontario, Canada
    Posts
    17
    Quote Originally Posted by laserlight View Post
    Perhaps you would be better off creating the large vector of objects (not pointers) in a function, do the processing in that function, and then return (perhaps via an out parameter) the smaller subset result, leaving the temporary large data set to be destroyed automatically as control leaves the scope of the function...
    Good idea! I'm just learning to use the STL. One thing I don't understand: why do I need to pass a vector of pointers by reference, but I don't need to pass an array of pointers by reference? For example, this works fine:

    Code:
    void arrayfct(int* n) 
    { int i;
       for(i = 0; i < 5; i++) { n[i] = i; }
    }

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,159
    why do I need to pass a vector of pointers by reference, but I don't need to pass an array of pointers by reference?
    A vector, like all other objects, is passed by value, so if you fail to pass it by reference or pass a pointer to it, then the function operates on a copy of the vector instead of the vector itself. An array, on the other hand, is converted to a pointer to its first element when passed as an argument, so the function operates on the elements of the array itself, not a copy of them.
    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

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Because a vector is a single object, and an array is multiple objects (in this case integers).

    Similarly, if you wanted to pass an array of vectors, that would be passed as a single pointer to the first integer.

    Yes, it's sort of inconsistent, but you have to accept that's the way C is.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #12
    Registered User
    Join Date
    Jun 2008
    Location
    Toronto, Ontario, Canada
    Posts
    17
    Ahhh, I see now! Thank you. I just joined this group. It seems very active and helpful!

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, 05:23 AM
  2. Conversion of pointers to functions
    By hzmonte in forum C Programming
    Replies: 0
    Last Post: 01-20-2009, 01:56 AM
  3. array of function pointers ..
    By vlrk in forum C Programming
    Replies: 1
    Last Post: 07-24-2008, 04:08 AM
  4. Pointers trouble
    By saahmed in forum C Programming
    Replies: 39
    Last Post: 03-24-2006, 04:08 AM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM

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