Thread: Puzzling pointers...

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    15

    Puzzling pointers...

    Whats the difference between

    Code:
    void swap(int* a,int* b)
    {
    	int x=*a,y=*b;
    	x=x+y;
    	y=x-y;
    	x=x-y;
    	*a=x; *b=y;
    	return;
    }
    and...

    Code:
    void swap(int* a,int* b)
    {
    	*a=*a+*b;
    	*b=*a-*b;
    	*a=*a-*b;
    	return;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Whats the difference between
    Functionally, they both have the same net effect (presumably to perform a swap in a rather roundabout way, assuming no arithmetic overflow). The first version simulates call by copy-restore (a.k.a. value-result), the second just uses pointer arguments to simulate call by reference, as is the usual method in C.
    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

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Does it matter?
    They're both broken attempts to swap two integers.

    The first fails miserably in attempting to avoid creating one temporary, by instead creating two temporaries.

    Try calling the second one with swap(&a,&a)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Try calling the second one with swap(&a,&a)
    Oh yes, but that means that I am wrong to say that they have the same net effect. Call by copy-restore avoids this problem.
    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

  5. #5
    Registered User
    Join Date
    Aug 2008
    Posts
    15
    I implemented the second in my code and it didn't work...
    I replaced it with first and it works fine....

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I implemented the second in my code and it didn't work...
    A quick test shows that it works, with the caveat pointed out by Salem.

    I hope that you are actually implementing swap() for your actual code with the more straightforward implementation.
    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

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    How about just
    Code:
    int temp = *a;
    *a = *b;
    *b = temp;
    It does exactly what you think it should do, has no unexpected side effects, and doesn't rely on the overflow behaviour of your machine.

    Oh, and stop paying attention to dumb programming tricks.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

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, 04:23 AM
  2. Using pointers to pointers
    By steve1_rm in forum C Programming
    Replies: 18
    Last Post: 05-29-2008, 05:59 AM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 01:18 PM

Tags for this Thread