Velocity Reviews > K&R exercise 4-14

# K&R exercise 4-14

RoSsIaCrIiLoIA
Guest
Posts: n/a

 05-10-2004
On Sun, 09 May 2004 16:23:35 -0400, Kevin Zhou <(E-Mail Removed)>
wrote:

>Nick Austin wrote:
>> On Sat, 08 May 2004 23:19:51 -0400, Kevin Zhou <(E-Mail Removed)>
>> wrote:
>>
>>
>>>http://users.powernet.co.uk/eton/kandr2/krx414.html
>>>
>>>#define swap(t,x,y) do{t z=x;x=y;y=z}while(0)

^;

#define SWAP(t, x, y) do{t z=(x); (x)=(y); (y)=z;}while(0)

SWAP(INT, x, y);

Gregory Pietsch
Guest
Posts: n/a

 05-13-2004
Kevin Zhou <(E-Mail Removed)> wrote in message news:<c7k6ko\$d59\$(E-Mail Removed)>...
> The question is "define a macro swap(t,x,y) that interchanges two
> arguments of type t".
> What does it mean? what are arugments of a type? I can't find an
> explanation in the book.

It means that the two arguments x and y have an undetermined type t.

I wrote some of the different macros on Richard Heathfield's site.
Here's a different solution I thought of that uses sizeof, so it's
category 1:

#define swap(t,x,y) myswap(sizeof(t),(unsigned char*)&(x),(unsigned
char*)&(y))
void myswap(unsigned long a,unsigned char *b,unsigned char *c)
{
while (a--) {
*b ^= *c;
*c ^= *b;
*b++ ^= *c++;
}
}

Gregory Pietsch

Sam Dennis
Guest
Posts: n/a

 05-14-2004
Gregory Pietsch wrote:
> void myswap(unsigned long a,unsigned char *b,unsigned char *c)

Not size_t a?

> {
> while (a--) {
> *b ^= *c;
> *c ^= *b;
> *b++ ^= *c++;
> }
> }

That's a really bad idea; not only does it fail to deal with the two
values being the same, but, should they share a single byte of their
respective representations, it has undesirable behaviour for the few
types where it's undefined at all.

--
++acr@,ka"

Gregory Pietsch
Guest
Posts: n/a

 05-15-2004
Sam Dennis <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)-berlin.de>...
> Gregory Pietsch wrote:
> > void myswap(unsigned long a,unsigned char *b,unsigned char *c)

>
> Not size_t a?

It probably comes later on in K&R2, or was a brain cramp on my fault.

>
> > {

if (b != c)
> > while (a--) {
> > *b ^= *c;
> > *c ^= *b;
> > *b++ ^= *c++;
> > }
> > }

>
> That's a really bad idea; not only does it fail to deal with the two
> values being the same, but, should they share a single byte of their
> respective representations, it has undesirable behaviour for the few
> types where it's undefined at all.

Then how would you actually accomplish this?

Sam Dennis
Guest
Posts: n/a

 05-16-2004
Gregory Pietsch wrote:
> Sam Dennis <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)-berlin.de>...
>> Gregory Pietsch wrote:
>> > void myswap(unsigned long a,unsigned char *b,unsigned char *c)

>>
>> That's a really bad idea;

>
> Then how would you actually accomplish this?

The same way as elsewhere in this thread (with intentional and
unintentional mistakes corrected): with a temporary variable.

--
++acr@,ka"

Gregory Pietsch
Guest
Posts: n/a

 05-21-2004
Sam Dennis <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)-berlin.de>...
> Gregory Pietsch wrote:
> > Sam Dennis <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)-berlin.de>...
> >> Gregory Pietsch wrote:
> >> > void myswap(unsigned long a,unsigned char *b,unsigned char *c)
> >>
> >> That's a really bad idea;

> >
> > Then how would you actually accomplish this?

>
> The same way as elsewhere in this thread (with intentional and
> unintentional mistakes corrected): with a temporary variable.

So here's what you're telling me:

If the areas pointed to by b and c overlap, I should use a temporary
variable to swap the contents of b and c:

void myswap(size_t a, unsigned char *b, unsigned char *c)
{
unsigned char uc;

if (a && b && c)
while (a--) {
uc = *b;
*b = *c;
*c = uc;
b++, c++;
}
}

My point is that if the two areas overlap, you can't swap no matter
what algorithm you use. (Think about it for a second.)

Gregory Pietsch

Sam Dennis
Guest
Posts: n/a

 05-21-2004
Gregory Pietsch wrote:
>> > Sam Dennis <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)-berlin.de>...
>> >> Gregory Pietsch wrote:
>> >> > void myswap(unsigned long a,unsigned char *b,unsigned char *c)
>> >> > [xor bytewise]
>> >>
>> >> That's a really bad idea;

>
> So here's what you're telling me:
>
> If the areas pointed to by b and c overlap, I should use a temporary
> variable to swap the contents of b and c:

Where did you get that from? What I'm trying to say is that the xor
hack is unreliable under the best of circumstances and that applying
it to object representations (of an unknown type, no less) is asking
for trouble.

If you absolutely need a function of this sort (for swapping arrays,
or somesuch, I guess), it's probably most efficient to have a buffer
local to the function of a reasonable size for a loop of three calls
to memcpy, analogous to the three assignments in a swapping macro.

What the hell would swapping objects that overlap mean, anyway? It's
utter nonsense!

--
++acr@,ka"