Generic swap - comments and questions

Gregory L. Galloway gg10 at prism.gatech.EDU
Wed Jun 13 02:34:37 AEST 1990


In article <1990Jun12.120637.19933 at cs.utk.edu>, wozniak at utkux1.utk.edu (Bryon Lape) writes:
> 
> 	Thanks for the help in writing the generic swap routine.  I did
> have a mistake in the letter.  The secong time that the generic_swap()
> function was given (the header that is) should have read:
> 		generic_swap(void *a, void *b)
> Sorry for this oversight.
> 
> 	I do have a couple of questions, though...

I think someone already mentioned that this was really a waste of time.
Maybe he wasn't clear as to why.  In C you have access to pointers so you
would not want to actually move data from one place to another.  Instead you
would swap a pointer to the data.  Small values like int's could be swapped
since they could be smaller than a pointer, a pointer would be a waste. But
you don't need a generic swap function for each int, float, etc.   Just swap
them.  Large structures, arrays, character strings are best left where they 
are, swap the pointer.  The most common need to swap something is while 
performing a bubble sort or the like.  If your list is arranged as a linked
list already with pointers, just swap the pointers.  There's nothing really
tough here that requires 286 assembly, C++, and ANSI is final already and
no generic swap was added.

Greg Galloway
gg10 at prism.gatech.edu



More information about the Comp.std.c mailing list