brain teasers

K

Keith Thompson

saki said:
a=a+b;
b=a-b;
a=a-b;
however we may not be able to swap pointers this way.

Somebody already suggested this. For signed integers, it doesn't work
if there's an overflow. For floating-point numbers, it can fail due
to either overflow or rounding errors. For anything non-numeric, it
just doesn't work.
 
M

Martin Ambuhl

saki said:
a=a+b;
b=a-b;
a=a-b;
however we may not be able to swap pointers this way.

Or even integers, whether short or long or long long, whether signed or
unsigned,
or floating point numbers, whether float or double or long double.
There is _no_ type for which yours is an answer. That makes it worth
even less than the silly XOR trick.
 
D

Daniel Rudy

At about the time of 4/6/2007 10:34 AM, Chris Torek stated the following:
I think you left out an important sentence here (maybe you did not
realize it was important): his tools put in a tab (as in '\t')
character.

This tab came through fine on *my* usenet server, so that the
inside of the loop was more-indented. It appears that whatever
software sits between you and Daniel Rudy, however, ate the tab.
The fact that some Usenet servers (and/or other Usenet article
delivery systems) eat tabs are why I have my software run all
my postings through the "expand" program by default.

When I write my code on the Unix system, this is how I indent my code:

void swap(void *a, void *b, size_t size)
{
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;
*d1 = *d2;
*d2 = tmp;
if (i < (size - 1))
{
d1++;
d2++;
}
}
return;
}

When I code on the fly here in the group, it's just easier to do it this
way:

#define MAXSWAP 32 /* maximum size of variable swap */
int swap(void *a, void *b, int size)
{
char temp[MAXSWAP]; /* temp buffer */

if (size > MAXSWAP) return(-1);
memcpy(temp, a, size);
memcpy(a, b, size);
memcpy(b, temp, size);
return(0);
}


Now if your software is stripping tabs off, there's not much I can do
about it, Richard Heathfield.

--
Daniel Rudy

Email address has been base64 encoded to reduce spam
Decode email address using b64decode or uudecode -m

Why geeks like computers: look chat date touch grep make unzip
strip view finger mount fcsk more fcsk yes spray umount sleep
 
D

Daniel Rudy

At about the time of 4/6/2007 1:35 PM, Default User stated the following:
Possibly, I suppose. The indents were pretty small though.



Brian

About 2 space indents per block level.

#include <stdio.h>

int main(void)
{
printf("Hello World\n");
return(0);
}



--
Daniel Rudy

Email address has been base64 encoded to reduce spam
Decode email address using b64decode or uudecode -m

Why geeks like computers: look chat date touch grep make unzip
strip view finger mount fcsk more fcsk yes spray umount sleep
 
R

Richard Heathfield

Daniel Rudy said:

Now if your software is stripping tabs off, there's not much I can do
about it, Richard Heathfield.

Chris Torek has already explained how /he/ does something about it.

I stopped using tabs in C source years ago - I cannot now remember
whether Usenet was a factor in that decision, but it is not a decision
I have ever regretted.
 
K

Keith Thompson

Martin Ambuhl said:
Or even integers, whether short or long or long long, whether signed
or unsigned,
or floating point numbers, whether float or double or long double.
There is _no_ type for which yours is an answer. That makes it worth
even less than the silly XOR trick.

I think it actually works for unsigned integers.
 
F

Flash Gordon

Daniel Rudy wrote, On 07/04/07 18:02:

return(0);
}


Now if your software is stripping tabs off, there's not much I can do
about it, Richard Heathfield.

It almost certainly is not Richard's software but some news servers. It
may well not even be the news server Richard uses, merely some news
server between you and Richard.

The point is that it is a known problem that tabs are sometimes stripped
and so using tabs is a bad idea if you want your code to appear
formatted and/or you want people to read your posts.
 
R

Richard Heathfield

Joe Wright said:
Not if a=a+b; would wrap.

Well, let's see, using 8-bit unsigned integer types.

a = 200;
b = 56;

a = a + b; a -> 0
b = a - b; b -> 200
a = a - b; b -> 56

Looks good to me. (That is, I am suggesting that it does in fact work
for unsigned integer types. I am not trying to suggest that it is a
good technique.)
 
D

Default User

Richard said:
Daniel Rudy said:



Chris Torek has already explained how he does something about it.

I stopped using tabs in C source years ago - I cannot now remember
whether Usenet was a factor in that decision, but it is not a
decision I have ever regretted.


It was mandated by the first coding standard I worked to, but I can't
recall if I had come the same conclusion independently or not. I think
it's is the sensible approach, and many if not most text editors can be
set to do that, so it's really very little trouble.



Brian
 
J

Joe Wright

Richard said:
Joe Wright said:


Well, let's see, using 8-bit unsigned integer types.

a = 200;
b = 56;

a = a + b; a -> 0
b = a - b; b -> 200
a = a - b; b -> 56

Looks good to me. (That is, I am suggesting that it does in fact work
for unsigned integer types. I am not trying to suggest that it is a
good technique.)
Yeah, I know. I was too fast on the 'send' switch. Apologies to Keith
and you and you and you.. :)
 
I

Ian Collins

user923005 said:
Right, hence my comment:
"You are right in the case of fundamental types like integers and
doubles."
Or any struct that can be copied with a shallow copy.
 
D

Dik T. Winter

> Daniel Rudy wrote, On 07/04/07 18:02: ....
>
> It almost certainly is not Richard's software but some news servers. It
> may well not even be the news server Richard uses, merely some news
> server between you and Richard.

It almost certainly is the software. I surmise that Richard's software has
no tabstops set, and the software is just expanding tabs to the nearest
tabstop (which there is not), and so gets deleted on a reply and is not
seen when reading. See also very old discussions in net.lang.c about the
use of tabs in posted articles. When the reader uses tabstops different
from the poster's setting, the article can come out pretty mangled.

The use of tabs can be problematical for this reason, and that is also
why (I think) Default User had to expand tabs to spaces in his coding.
 
D

Default User

Dik said:
It almost certainly is the software. I surmise that Richard's
software has no tabstops set, and the software is just expanding tabs
to the nearest tabstop (which there is not), and so gets deleted on a
reply and is not seen when reading. See also very old discussions in
net.lang.c about the use of tabs in posted articles. When the reader
uses tabstops different from the poster's setting, the article can
come out pretty mangled.

The use of tabs can be problematical for this reason, and that is also
why (I think) Default User had to expand tabs to spaces in his coding.

Originally it was mandated by the common coding standard I working to,
but I certainly saw the wisdom of it. I use spaces instead of tabs in
all my code, personal or otherwise, these days. All my text
editors/IDEs can substitute spaces for tab key strokes or auto-indents
when requested, which makes that easier.



Brian
 
R

Randy Howard

I suspect it is historic. At one point -Wall enabled all. Then
more were needed. In order to not disturb old makefiles, -W was
added.

Adding them both still does not yield all the warnings that are
available.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top