Puzzling pointers...

This is a discussion on Puzzling pointers... within the C Programming forums, part of the General Programming Boards category; 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... ...

  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
    21,888
    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.
    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

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,888
    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.
    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

  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
    21,888
    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.
    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

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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, 09:33 AM
  4. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 12:18 PM

Tags for this Thread


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