Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > K&R exercise 4-14

Reply
Thread Tools

K&R exercise 4-14

 
 
RoSsIaCrIiLoIA
Guest
Posts: n/a
 
      05-10-2004
On Sun, 09 May 2004 16:23:35 -0400, Kevin Zhou <>
wrote:

>Nick Austin wrote:
>> On Sat, 08 May 2004 23:19:51 -0400, Kevin Zhou <>
>> 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);
 
Reply With Quote
 
 
 
 
Gregory Pietsch
Guest
Posts: n/a
 
      05-13-2004
Kevin Zhou <> wrote in message news:<c7k6ko$d59$>...
> 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
 
Reply With Quote
 
 
 
 
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"
 
Reply With Quote
 
Gregory Pietsch
Guest
Posts: n/a
 
      05-15-2004
Sam Dennis <> wrote in message news:<>...
> 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?
 
Reply With Quote
 
Sam Dennis
Guest
Posts: n/a
 
      05-16-2004
Gregory Pietsch wrote:
> Sam Dennis <> wrote in message news:<>...
>> 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"
 
Reply With Quote
 
Gregory Pietsch
Guest
Posts: n/a
 
      05-21-2004
Sam Dennis <> wrote in message news:<>...
> Gregory Pietsch wrote:
> > Sam Dennis <> wrote in message news:<>...
> >> 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
 
Reply With Quote
 
Sam Dennis
Guest
Posts: n/a
 
      05-21-2004
Gregory Pietsch wrote:
>> > Sam Dennis <> wrote in message news:<>...
>> >> 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"
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
tree functions daily exercise: Range Xah Lee Java 12 06-22-2005 08:51 AM
Cisco Student VPN exercise problem : gen_unrfrag: fail to generate unreachable, unexpected args robert Cisco 0 06-02-2004 07:33 PM
2154 module 4 Exercise 2 Drew Brown MCSE 0 10-22-2003 02:47 AM
Exercise needed for java 2 programmer test lonelyplanet999 Java 1 09-30-2003 10:37 AM
Re: Development best practices and knowing when to exercise control over development Kevin Spencer ASP .Net 2 08-06-2003 09:33 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57