Comment On A Common C Dilemma

How to prevent for-loops from boundary overrun? [expand full text]
« PrevPage 1 | Page 2Next »

Re: A Common C Dilemma

2009-04-01 08:48 • by amischiefr
Ahh you youngsters probably don't remember the for-while loop. The good olde days.

Re: A Common C Dilemma

2009-04-01 08:50 • by José Rodrigues (unregistered)
This code ain't for no boundary overrun prevention, nossir! It's actually a very clever, ingenious, way to add a NOP on the program, so that the snipped become 32bit-aligned!

Re: A Common C Dilemma

2009-04-01 08:52 • by FromCanada (unregistered)
I always use these when I only want my code to run for a while.

Re: A Common C Dilemma

2009-04-01 08:57 • by Pim
I see the article contains 138 characters in total.
Neat.
This comment has even fewer.

Re: A Common C Dilemma

2009-04-01 09:02 • by Kiss me I'm Polish
It took me a few moments to understand the "Gordon's c/w came up w/ this".

Re: A Common C Dilemma

2009-04-01 09:04 • by Kardec (unregistered)
It's too long this way! Just take away some vowels, nobody will notice. :-)

Re: A Common C Dilemma

2009-04-01 09:09 • by SlyEcho
I once wrote this for loop:

for (byte b = 0; b <= 255; b++) {
// code
}

Re: A Common C Dilemma

2009-04-01 09:11 • by SomeC++Dude (unregistered)
252815 in reply to 252800
Re:José Rodrigues

a good compiler would optimize that instruction out

Re: A Common C Dilemma

2009-04-01 09:13 • by Zapp Brannigan (unregistered)
252816 in reply to 252813
SlyEcho:
I once wrote this for loop:

for (byte b = 0; b <= 255; b++) {
// code
}
I haven't used C for a while so I had to think for a second. That's nice.

Re: A Common C Dilemma

2009-04-01 09:15 • by OSvsOS
252817 in reply to 252815
"a good compiler would optimize that instruction out"

A bad programmer would forget to use the good compiler's optimization flags.

Re: A Common C Dilemma

2009-04-01 09:29 • by Patriot (unregistered)
Good thing you abbreviated co-worker and with, the story was so long it started to get boring.

Re: A Common C Dilemma

2009-04-01 09:37 • by Flanged Magnet (unregistered)
252826 in reply to 252816
You remembered that only fools assume bytes are 8 bits?

Re: A Common C Dilemma

2009-04-01 09:41 • by fist-poster (unregistered)
while (1); or any other number probably didn't work, but 0 appeared to correct the problem.

Re: A Common C Dilemma

2009-04-01 09:46 • by ledhund (unregistered)
252834 in reply to 252816
noob question: the byte is signed, right?

Re: A Common C Dilemma

2009-04-01 09:53 • by fanguad (unregistered)
tldr

Re: A Common C Dilemma

2009-04-01 09:55 • by ross (unregistered)
252845 in reply to 252834
ledhund:
noob question: the byte is signed, right?

It's probably not signed. But it is always going to be less than or equal to 255. Since the range of values is 0~255

Re: A Common C Dilemma

2009-04-01 09:57 • by C4I_Officer
Heh, I had a boundary overflow problem once or twice with Delphi code.

I wrote this apparently simple snippet of code:

for i:=start to end do
// code

which however appeared to freeze everything, as if start > end when the loop is started it will apparently run forever...or for very long, depending on what i is (an Integer however will take pretty long...). Why? Because Delphi implies only a precise equality as the loop ending condition. No "greater or equal" here. The solution was to either replace the for loop with a "while (condition) do" construct, or explicitly checking for the start>end condition before the if.

Re: A Common C Dilemma

2009-04-01 09:58 • by AlpineR
What are you doing?
Rdng TDWTF n lol @ code

Re: A Common C Dilemma

2009-04-01 09:58 • by jimlangrunner
252850 in reply to 252834
ledhund:
noob question: the byte is signed, right?

Thinking here. I never did do C, but I did teach C++ once on TV.

The byte is NOT signed, but it will overflow (the b++ overflows the byte), so it should go to 0. Infinite loop.

Nice.
Jim.

Re: A Common C Dilemma

2009-04-01 10:02 • by ledhund (unregistered)
252852 in reply to 252845

jimlangrunner:
ledhund:
noob question: the byte is signed, right?

Thinking here. I never did do C, but I did teach C++ once on TV.

The byte is NOT signed, but it will overflow (the b++ overflows the byte), so it should go to 0. Infinite loop.

Nice.
Jim.


doh! I didn't read the = so imagined the overflow already at 127++

Re: A Common C Dilemma

2009-04-01 10:15 • by Zapp Brannigan (unregistered)
252860 in reply to 252826
Flanged Magnet:
You remembered that only fools assume bytes are 8 bits?
Also old farts like me. But then it's not an exclusive or.

Re: A Common C Dilemma

2009-04-01 10:48 • by blank (unregistered)
tl;dr

Re: A Common C Dilemma

2009-04-01 11:16 • by lolwtf
252916 in reply to 252809
Kiss me I'm Polish:
It took me a few moments to understand the "Gordon's c/w came up w/ this".
I'm scared that I read and understood it without even realizing they were abbreviated. o_o

Re: A Common C Dilemma

2009-04-01 11:18 • by Ryan (unregistered)
252918 in reply to 252813
I did that once. Took me far too long to figure out why it wouldn't end.

Re: A Common C Dilemma

2009-04-01 11:31 • by lolcoder (unregistered)
252929 in reply to 252916
lolwtf:
Kiss me I'm Polish:
It took me a few moments to understand the "Gordon's c/w came up w/ this".
I'm scared that I read and understood it without even realizing they were abbreviated. o_o


Reminds me of this fortune:
[Temperature 13 C][16:28:13 01-April-09][/home/adam]
[adam@owl]$ fortune -m "f u cn"
%% (fortunes)
f u cn rd ths, u cn gt a gd jb n cmptr prgrmmng.

Re: A Common C Dilemma

2009-04-01 11:42 • by Maurits
252935 in reply to 252826
Flanged Magnet:
You remembered that only fools assume bytes are 8 bits?


Don't be silly. Eight bits is a dollar.

Re: A Common C Dilemma

2009-04-01 11:48 • by Evo
252938 in reply to 252813
SlyEcho:
I once wrote this for loop:

for (byte b = 0; b <= 255; b++) {
// code
}


I once saw this while loop:

int n = 0;
while(n >= 0) n++;


G++ with -O3 turned it into an infinite loop. It's not even a bug.

Re: A Common C Dilemma

2009-04-01 12:00 • by GettinSadda
252948 in reply to 252813
My favourite is always this one - I keep finding that my finigers typo it!

for(int i=0; i<end; i++); 

{
// code
}

Re: A Common C Dilemma

2009-04-01 12:13 • by MrEricSir
At first I thought I was looking at a for...while loop. But it turns out the ; substitutes for the body of the while loop, so it's exactly the same as:

for (int i=0;i<max; i++) {

...
}

while(0) {}


Plus this way it works in Java as well as C.

Re: A Common C Dilemma

2009-04-01 12:47 • by Ardax (unregistered)
252974 in reply to 252848
Um, no. While the terminating condition of a for loop is strict equality in Delphi, the runtime also checks that Start <= End. If Start > End, then it will skip the loop altogether. Also, End is only evaluated once for for loops in Delphi (e.g. If End is really a function call, the function is only called once at the first check.). For a while loop, the condition is evaluated for every iteration.

Ergo, checking for Start > End before entering a for loop doesn't do anything unless End is a function call with a side effect, and while loops have subtly different semantics that can make a difference.

Re: A Common C Dilemma

2009-04-01 13:10 • by WillowAnne (unregistered)
252983 in reply to 252798
the e in olde: extra character!

Re: A Common C Dilemma

2009-04-01 14:12 • by RobFreundlich
I call Shenanigans. The
while(0)
was very obviously and poorly Photoshopped in.

Re: A Common C Dilemma

2009-04-01 14:33 • by RogerC
It's too long to re-tweet.

Re: A Common C Dilemma

2009-04-01 15:02 • by C4I_Officer
253010 in reply to 252974
Um, no. While the terminating condition of a for loop is strict equality in Delphi, the runtime also checks that Start <= End. If Start > End, then it will skip the loop altogether. Also, End is only evaluated once for for loops in Delphi (e.g. If End is really a function call, the function is only called once at the first check.). For a while loop, the condition is evaluated for every iteration.


Delphi 7, d'oh. And it just froze when ran: debugging the loop revealed that the index variable was merrily stepping its way up to MAXINT and then from -MAXINT...go figure.

Start and End were variables in my case, and I used the default project options. It sure made me go "WTF" when I realized it, and chose the easiest workaround, since I really needed the index variable inside the loop, and felt that increasing them myself just made the whole code look uglier.

Re: A Common C Dilemma

2009-04-01 15:31 • by a guy (unregistered)
253015 in reply to 252805
Everybody's routing through Twitter now.

Re: A Common C Dilemma

2009-04-01 16:27 • by CoyneT (unregistered)
One of the nastiest practical jokes I ever read about was how to totally confuse C programmers. To do this, you bury this DEFINE in some ob

Re: A Common C Dilemma

2009-04-01 16:27 • by CoyneT (unregistered)
scure header file:
#DEFINE do if

Re: A Common C Dilemma

2009-04-01 19:20 • by dwilliss
I once did this...

    #define EverAndEver ;;

Then you can do...

    for (EverAndEver) {  

}

A coworker saw it and added the comment:

    for (EverAndEver) {  // Hallelujah, Hallelujah!

}

Re: A Common C Dilemma

2009-04-01 21:07 • by Jim (unregistered)
Actually, if you count all new lines its 142 characters, and if this was encoded in windows format, its more like 149 characters. There's the real WTF.

Re: A Common C Dilemma

2009-04-02 05:17 • by Random guy (unregistered)
253118 in reply to 252938
Evo:
I once saw this while loop:

int n = 0;
while(n >= 0) n++;


G++ with -O3 turned it into an infinite loop. It's not even a bug.

Hey, it works just as coded. :-)

Re: A Common C Dilemma

2009-04-02 05:30 • by pjt33
253119 in reply to 253118
Random guy:
Evo:
I once saw this while loop:

int n = 0;
while(n >= 0) n++;


G++ with -O3 turned it into an infinite loop. It's not even a bug.

Hey, it works just as coded. :-)

Only on slow machines.

Re: A Common C vs. Java Dilemma

2009-04-02 06:04 • by SCJP (unregistered)
253121 in reply to 252955
MrEricSir:
while(0) {}


Plus this way it works in Java as well as C.


No. In Java it doesn't. Boolean values in Java are really true or false, not 0 or !0.

Re: A Common C Dilemma

2009-04-02 08:40 • by joelkatz
If you put a newline before the while(0); it will be immediately obvious what this code does. But when you first look at it, it does make you go "huh".

It is a fair criticism of C/C++ that non-standard indentation can mislead you as to what the code is doing. This code is a good example of that.

Re: A Common C Dilemma

2009-04-02 10:12 • by José (unregistered)
Ouch... yet another python fanatic

Re: A Common C Dilemma

2009-04-02 12:43 • by Cbuttius (unregistered)
This was also posted on April 1st but there is no such thing as a for...while construct and they are two separate loops in the code.

Re: A Common C Dilemma

2009-04-03 08:35 • by pbi (unregistered)
This is C++ mate :). Strictly, you can only declare variables in the beginning of a code block / function and not in the head of a for loop (for (int i = 0 ...). Thats wtf :P

Re: A Common C Dilemma

2009-04-03 09:12 • by pbi (unregistered)
253429 in reply to 253419
pbi:
This is C++ mate :). Strictly, you can only declare variables in the beginning of a code block / function and not in the head of a for loop (for (int i = 0 ...). Thats wtf :P


EDIT: ..Strictly speaking, you can only declare variables ....

Re: http://www ...

2009-04-05 07:40 • by Da' Man (unregistered)
ugg boots:
...

And your Captcha was..?

Re: A Common C Dilemma

2009-04-06 08:53 • by AB (unregistered)
I'm sure the performance boost was amazing too

Re: A Common C Dilemma

2009-04-06 22:04 • by runescape gold (unregistered)
Most importantly, lonely from obtains warm with the runescape glod person contact and to person's new understanding. I knew that these words are specious. Perhaps some people will ask: The runescape money lonely person, is not because the lonely essence is very only then difficult with the human to associate? Actually just right opposite, lonely person especially qualify and with others establishment relations. The runescape accounts reason is very simple: Because lonely. In the impression had a mind the scientist once to make such elaboration probably:"lonely not necessarily conflicts with the contact, because does not have one to be able to need the companion compared to the lonely person."
« PrevPage 1 | Page 2Next »

Add Comment