[comp.lang.c] Generic swap - comments and questions

wozniak@utkux1.utk.edu (Bryon Lape) (06/12/90)

	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.  First, one of the
responses received used memcpy() to move the data around.  This is fine
if both items have the same length, but does not work of different
lengths (such as two different size strings, say the names of two
people).  Is there anyway to build this routine so that it will change
data no matter what the lengths are?  Second, does anyone know if ANSI
will add a generic swap function to the C library?  Third, is it
possible to write this better in machine language (80286)?  Last, what
of C++?  Better to write it here?


-bryon lape-

gg10@prism.gatech.EDU (Gregory L. Galloway) (06/12/90)

In article <1990Jun12.120637.19933@cs.utk.edu>, wozniak@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@prism.gatech.edu