Re: brain teasers



Daniel Rudy said:

<snip>

Man, you guys are flat ruthless on my coding algorithms and style. My
original one was shot down, then my malloc one caused an argument...

Don't you think that's a good thing?

All of us have been shot down in flames a few times here in c.l.c, and
it has generally been a useful experience.

In
taking everyone's suggestions into account. I have devised the
following routine:

void swap(void *a, void *b, size_t size)

You'll need a header for that size_t.

{
size_t i; /* generic counter */
unsigned char tmp; /* temp */
unsigned char *d1, *d2; /* data pointers */

d1 = a;
d2 = b;
for (i = 0; i < size; i++)
{
tmp = *d1;

Your indenting is broken. <g,d&rvf>

That one doesn't use malloc, swaps 1 byte at a time, and can be used
with any datatype. It will not increment d1 and d2 beyond the size
either.

I think you could profitably swap more bytes at a time than that, using
memcpy. Who told you swapping many bytes at once was a bad idea?

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
.



Relevant Pages

  • Re: On the development of C
    ... snip ... ... library like malloc, for instance, where a function to know the ... void *newmalloc{ ... The resulting pointer may not be properly aligned for all types so ...
    (comp.lang.c)
  • Re: On the development of C
    ... Bartc wrote: ... snip ... ... library like malloc, for instance, where a function to know the ... void *newmalloc{ ...
    (comp.lang.c)
  • Re: On the development of C
    ... snip ... ... library like malloc, for instance, where a function to know the ... millions of malloc pointers to be managed, ... void *newmalloc{ ...
    (comp.lang.c)
  • Re: Railway Photography!
    ... If you want to take a general distant shot ... station from an overbridge, say, how on earth are you expected not to ... incidentally and/or unidentifiably included in a shot. ...
    (uk.railway)
  • Re: Railway Photography!
    ... station from an overbridge, say, how on earth are you expected not to get ... incidentally and/or unidentifiably included in a shot. ... It is unacceptable to photograph individuals in private places without their ...
    (uk.railway)