Swapping pointers

This is a discussion on Swapping pointers within the C++ Programming forums, part of the General Programming Boards category; I have a bit of code that I'm having some trouble with. It looks something like this (I broke it ...

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

    Question Swapping pointers

    I have a bit of code that I'm having some trouble with. It looks something like this (I broke it down to make it short and sweet):
    Code:
    #include <vector>
    #include <string>
    
    class MyClass
    {
    public:
    	std::string some_string;
    	double some_double;
    
    	MyClass() : some_string(""), some_double(0.0){}
    };
    
    void function(std::vector <MyClass>*, std::vector <MyClass>*);
    
    int main(void)
    {
    	std::vector <MyClass>* first_object = new std::vector <MyClass>;
    	std::vector <MyClass>* second_object = new std::vector <MyClass>;
    	function(first_object, second_object);
    
    	delete first_object;
    	first_object = NULL;
    	delete second_object;
    	second_object = NULL;
    
    	return 0;
    }
    
    void function(std::vector <MyClass>* first_object, std::vector <MyClass>* second_object)
    {
    	//Do stuff to first_object.
    	//...
    	//Do stuff to second_object.
    	//...
    
    	*first_object = *second_object;
    }
    The problem is, *first_object and *second_object can both be very large, and function() can be called many times. The assignment at the end of function() is creating an enormous bottleneck in my program. After the assignment is done, I'm only concerned about the contents of *first_object; second_object isn't used outside of function() and it's contents are re-written in function() each call. So I figure an easy way to speed up execution would be to swap the pointers rather than do the costly assignment. So, at the end of function(), I want to do something like this:

    Code:
    	std::vector <MyClass>* temp_ptr = first_object;
    	first_object = second_object;
    	second_object = temp_ptr;
    However, that code isn't doing what I want it to, and for the life of me, I can't figure out why. Any words of advice?
    Last edited by Mostly Harmless; 11-29-2008 at 11:13 PM.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    vector has a swap member function. You can use that. Swapping pointers won't help here because it only changes what the pointers point to in the function, not what the pointers point to in the calling code. You could pass the pointers by reference to accomplish this, but there's no need since vector has a swap method.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,294
    I realise that this is a cut down piece of code, but if you're doing this here, you're almost certainly doing it in your main code.
    The issue is that you should pretty much never dynamically create a vector. Think of a vector as a pointer to an array. Why would you dynamically allocate that pointer itself?! (okay it's actually typically 3 pointers but no diff)
    The array inside the vector is already dynamically allocated. Then to pass them to another function, you do so by reference (const if that function doesn't modify them)

    Here's a corrected and simplified version of your sample program:
    Code:
    #include <vector>
    #include <string>
    
    class MyClass
    {
    public:
    	std::string some_string;
    	double some_double;
    
    	MyClass() : some_double() {}
    };
    
    void function(std::vector<MyClass> &, std::vector<MyClass> &);
    
    int main(void)
    {
    	std::vector<MyClass> first_object, second_object;
    	function(first_object, second_object);
    
    	return 0;
    }
    
    void function(std::vector<MyClass> &first_object, std::vector<MyClass> &second_object)
    {
    	//Do stuff to first_object.
    	//...
    	//Do stuff to second_object.
    	//...
    
    	first_object.swap(second_object);
    }
    I could tell you why your original version didn't work, but that would probably lead you furthur away from how it should be implemented.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,428
    Last edited by Elysia; 11-30-2008 at 11:26 AM.
    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.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,294
    Quote Originally Posted by Elysia View Post
    Actually I second that, as it had actually confused me for a second when I edited the original code.
    I nearly decided to reorder it anyway so that function came before main, eliminating the need for the prototype.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    Registered User
    Join Date
    Aug 2006
    Posts
    43
    I agree with using the variable names in the function prototypes. It certainly makes the code easier to understand.

    Quote Originally Posted by iMalc View Post
    I realise that this is a cut down piece of code, but if you're doing this here, you're almost certainly doing it in your main code.
    The issue is that you should pretty much never dynamically create a vector. Think of a vector as a pointer to an array. Why would you dynamically allocate that pointer itself?! (okay it's actually typically 3 pointers but no diff)
    The array inside the vector is already dynamically allocated. Then to pass them to another function, you do so by reference (const if that function doesn't modify them)

    Here's a corrected and simplified version of your sample program:
    Code:
    snip
    I could tell you why your original version didn't work, but that would probably lead you furthur away from how it should be implemented.
    I almost asked about dynamically creating vectors in my original question, because it didn't seem right. The first draft of my program used references instead of pointers, but the swapping of vectors seemed slow (however, I wasn't using the swap() member function ... I'll try that now). In my actual program, the class can be rather large, and the vector can also be quite big (somewhere in the hundreds). I'm also calling "function()" many, many times. My thinking was that the swapping of pointers would mean exchanging an extremely small value, rather than a gigantic vector of stuff. Is my thinking way off? If so, why?

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,294
    Quote Originally Posted by Mostly Harmless View Post
    I agree with using the variable names in the function prototypes. It certainly makes the code easier to understand.


    I almost asked about dynamically creating vectors in my original question, because it didn't seem right. The first draft of my program used references instead of pointers, but the swapping of vectors seemed slow (however, I wasn't using the swap() member function ... I'll try that now). In my actual program, the class can be rather large, and the vector can also be quite big (somewhere in the hundreds). I'm also calling "function()" many, many times. My thinking was that the swapping of pointers would mean exchanging an extremely small value, rather than a gigantic vector of stuff. Is my thinking way off? If so, why?
    Your concern about not wanting to swap by copying a large number of large items is a very valid concern. Fortunately it is a very common concern, and as such a method exists in the vector class to perform this function very efficiently. It just internally swaps the pointers to their internal dymanically allocated memory around.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. swapping pointers
    By csvraju in forum C Programming
    Replies: 17
    Last Post: 04-01-2009, 03:18 AM
  2. Swapping Pointers & Arrays
    By bartybasher in forum C++ Programming
    Replies: 6
    Last Post: 10-25-2003, 02:17 PM
  3. Swapping string char with pointers
    By Black-Hearted in forum C++ Programming
    Replies: 4
    Last Post: 06-18-2003, 05:36 AM
  4. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 12:18 PM
  5. Swapping pointers
    By VanJay011379 in forum C++ Programming
    Replies: 3
    Last Post: 07-30-2002, 12:57 PM

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