swap function - generic use?

Thread Solved

Join Date: Aug 2007
Posts: 11
Reputation: xyster is an unknown quantity at this point 
Solved Threads: 0
xyster xyster is offline Offline
Newbie Poster

swap function - generic use?

 
0
  #1
May 17th, 2008
I have a quick question that is been bugging me.

  1. void
  2. swap(void *a, void *b)
  3. {
  4. void *tmp;
  5. tmp = a;
  6. a = b;
  7. b = tmp;
  8. }

I wrote this intending it to be either able to swap a structure, or just the data defined within a structure.

Will the swap function be able to do this, because.. when i tested it, it seemed to fail. but i didnt except it too.

it should atleast work for swapping the data stored within the structure though, shouldnt it? (the data is of type event, which is shown below the heap struct).


** here are the structures **
  1. struct heap_s
  2. {
  3. heap left, right;
  4. void* data;
  5. };
  6.  
  7. struct event_s {
  8. etime time;
  9. int e_num;
  10. void (*event_fn)(event, pqueue);
  11. void *details;
  12. };
Last edited by xyster; May 17th, 2008 at 7:24 am.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,397
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1466
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: swap function - generic use?

 
0
  #2
May 17th, 2008
Originally Posted by xyster View Post
I wrote this intending it to be either able to swap a structure, or just the data defined within a structure.

Will the swap function be able to do this, because.. when i tested it, it seemed to fail. but i didnt except it too.
It fails because all it does is swap the pointers within the swap() function itself because the pointers are passed by value, not by reference.

  1. void swap(void** a, void** b)
  2. {
  3. void* temp = *a;
  4. *a = *b;
  5. *b = temp;
  6. }

Now the above will work ONLY if you pass a pointer to a pointer. It will also fail if you pass a pointer to some object.
int main()
{
    int a = 1;
    int b = 2;
    swap(&a, &b); // <<<<<<< wrong

    int* c = &a;
    int* d = &b;
    swap(&c, &d); // <<< OK
// when the swap returns, pointer c will point to b and pointer d will point to a.
// The value of a and b are still the same, only the two pointers
// were swapped

it should atleast work for swapping the data stored within the structure though, shouldnt it?
No.
Last edited by Ancient Dragon; May 17th, 2008 at 9:48 am.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 11
Reputation: xyster is an unknown quantity at this point 
Solved Threads: 0
xyster xyster is offline Offline
Newbie Poster

Re: swap function - generic use?

 
0
  #3
May 17th, 2008
oh, i see what i was doing wrong.

thanks for the quick reply.

i have added the updates and passed it the memory address now as you showed.

you can add the lovely "Solved" heading to my topic. Thankyou so much for such a quick reply.

This board has been really useful.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC