Thread: Swapping pointers

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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?

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    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, 01:18 PM