Swap Integers

This is a discussion on Swap Integers within the C++ Programming forums, part of the General Programming Boards category; Now, a is local to the main function. You are trying to access it as a global variable in inc(). ...

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,461
    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;
    }
    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

  2. #17
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,238
    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
    21,461
    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
    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

  4. #19
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,238
    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
    21,461
    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"?
    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

  6. #21
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,238
    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
    21,461
    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.
    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

  8. #23
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,238
    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,006
    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
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,238
    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,006
    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
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,238
    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,006
    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
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,238
    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,893
    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

Page 2 of 5 FirstFirst 12345 LastLast
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, 05:55 PM
  2. Assignment HELP!!
    By cprogrammer22 in forum C Programming
    Replies: 35
    Last Post: 01-24-2009, 01: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, 05:32 AM
  5. Replies: 6
    Last Post: 08-04-2003, 10:57 AM

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