Thread: Swap Integers

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Now, a is local to the main function. You are trying to access it as a global variable in inc(). Try this instead:
    Code:
    #include <iostream>
    
    void inc(int* b)
    {
        *b += 50;
    }
    
    int main()
    {
        int a = 100;
        std::cout << "a = " << a << std::endl;
        inc(&a);
        std::cout << "a = " << a << std::endl;
    }
    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

  2. #17
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    You guys are so funny...

    Code:
    template <typename T>
    void swap(T &a, T &b)
    {
        T tmp;
        tmp = a;
        a = b;
        b = tmp;
    }
    Or for ints only:

    Code:
    void swap(int &a, int &b)
    {
        a^=b;b^=a;a^=b;
    }

  3. #18
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You guys are so funny...
    That's rather funny too, writing a function template already present in the standard library, and doing it rather inefficiently by not using the copy constructor
    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

  4. #19
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by laserlight View Post
    That's rather funny too, writing a function template already present in the standard library, and doing it rather inefficiently by not using the copy constructor
    I did that on purpose, since initializing tmp with the copy constructor, then doing the assignments via operator=, seemed asymmetrical to me.

  5. #20
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I did that on purpose, since initializing tmp with the copy constructor, then doing the assignments via operator=, seemed asymmetrical to me.
    Interesting way of viewing it. Going off topic here, but I suppose redneon's query on pointers has been answered: what do you mean by "asymmetrical"?
    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

  6. #21
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by laserlight View Post
    Interesting way of viewing it. Going off topic here, but I suppose redneon's query on pointers has been answered: what do you mean by "asymmetrical"?
    Well, what if operator= and the copy constructor do slightly different things?

  7. #22
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Well, what if operator= and the copy constructor do slightly different things?
    What if operator= does not actually assign? The way I see it, one has to assume certain preconditions are met before the swap works, and having the copy constructor and copy assignment operator do the same thing is one such condition.

    EDIT:
    Or perhaps, what if there is no default constructor? swap() is required to work on types that are copy constructible and assignable, with no requirement on being default constructible.
    Last edited by laserlight; 04-28-2007 at 10:43 AM.
    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

  8. #23
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by laserlight View Post
    What if operator= does not actually assign? The way I see it, one has to assume certain preconditions are met before the swap works, and having the copy constructor and copy assignment operator do the same thing is one such condition.
    Valid argument.

  9. #24
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by brewbuck View Post
    Or for ints only:

    Code:
    void swap(int &a, int &b)
    {
        a^=b;b^=a;a^=b;
    }
    That one's so icky it's just not worth mention.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  10. #25
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Dave_Sinkula View Post
    That one's so icky it's just not worth mention.
    Funny thing though, gcc on Intel actually optimizes that into a xchg instruction, at least if the variables are held in registers at that point. I don't dare to even consider what sort of machinery inside gcc is able to figure that out!

  11. #26
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Even at that it still has a bug in it.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  12. #27
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Dave_Sinkula View Post
    Even at that it still has a bug in it.
    I'm stumped.

  13. #28
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    The swap with self thingy.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  14. #29
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Dave_Sinkula View Post
    The swap with self thingy.
    Of course.

  15. #30
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Quote Originally Posted by brewbuck View Post
    Funny thing though, gcc on Intel actually optimizes that into a xchg instruction, at least if the variables are held in registers at that point. I don't dare to even consider what sort of machinery inside gcc is able to figure that out!
    I suppose they decided to fix the mistake "clever" programmers are making by using this. As for the machinery, it's nothing that isn't there to recognize far more complex patterns, such as loops that can/should be unrolled.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  2. Assignment HELP!!
    By cprogrammer22 in forum C Programming
    Replies: 35
    Last Post: 01-24-2009, 02:24 PM
  3. Integers into array.
    By livestrng in forum C Programming
    Replies: 10
    Last Post: 10-29-2008, 11:35 PM
  4. using swap to make assignment operator exception safe
    By George2 in forum C++ Programming
    Replies: 9
    Last Post: 01-10-2008, 06:32 AM
  5. Replies: 6
    Last Post: 08-04-2003, 10:57 AM