Print Story thanks everyone who congratulated me with a new job
Diary
By i (Fri Dec 22, 2006 at 02:14:25 AM EST) (all tags)
now to our little C puzzle.

ok, here's a function.


int safe_division (int a, int b)
{
  retun b ? (a / b) : 0;
}

is this code safe? is it? is it? i'm asking you!

well, cow could it be unsafe?



it is unsafe. it can and does crash on common architectures, such as IA32. honest. try to figure out that rare combination of inputs.
< Early Consumer-mas | BBC White season: 'Rivers of Blood' >
thanks everyone who congratulated me with a new job | 15 comments (15 topical, 0 hidden) | Trackback
uh by komet (2.00 / 0) #1 Fri Dec 22, 2006 at 02:32:17 AM EST
when you say "crash", do you mean it takes over your computer and installs spyware and a kiddy porn server?

--
<ni> komet: You are functionally illiterate as regards trashy erotica.
probably means by sasquatchan (2.00 / 0) #5 Fri Dec 22, 2006 at 04:50:27 AM EST
crash because someone didn't feel like writing correct code to catch exceptions and handle them in a graceful manner.

[ Parent ]
What kind of person by ambrosen (2.00 / 0) #7 Fri Dec 22, 2006 at 05:14:52 AM EST
Kernighan, I mean, can't even be bothered to check that sort of failure. Let alone building the ubiquitous processor architecture to fail on a division. Oh wait, it's a result of twos compliment negative numbers.

Another in the series of i's great teasers, I guess.

[ Parent ]
it's a ha-ha only serious by sasquatchan (2.00 / 0) #13 Fri Dec 22, 2006 at 05:23:17 AM EST
in that most code I deal with has a near top-level try/catch that catches (...) and usually just kills the thread. SEH has good and bad points, (most) everyone says "it(SEH) is a good thing," but then goes and writes code with no real exception handling.

(Mainly because 'doing it right' can easily triple your lines of code...)

[ Parent ]
well it could by i (4.00 / 1) #10 Fri Dec 22, 2006 at 05:22:13 AM EST
i'm not aware of any machine that actually does that, but you know, it's not prohibited by the standard.


[ Parent ]
Complete guess by TPD (2.00 / 0) #2 Fri Dec 22, 2006 at 03:11:36 AM EST
some wierd branch prediction issue forcing the division (and subsequent error flagging) when b is 0.

I'm really weak on this sort of stuff though so I am almost certainly talking out of my arse.

why sit, when you can sit and swivel with The Ab-SwivellerTM

nope /nt by i (2.00 / 0) #11 Fri Dec 22, 2006 at 05:22:45 AM EST



[ Parent ]
Arse by TurboThy (2.00 / 0) #3 Fri Dec 22, 2006 at 04:19:54 AM EST
#DEFINE retun crash_horribly(true)
__
Sommerhus til salg, første række til Kattegat.
that would be cheating. by i (2.00 / 0) #14 Fri Dec 22, 2006 at 05:23:27 AM EST
me no cheating!


[ Parent ]
Well by TurboThy (2.00 / 0) #15 Fri Dec 22, 2006 at 10:50:39 AM EST
What's the definition of retun then? ;)
__
Sommerhus til salg, første række til Kattegat.
[ Parent ]
man signal by lm (4.00 / 1) #4 Fri Dec 22, 2006 at 04:26:51 AM EST
blah, blah, blah, blah ... dividing the most negative integer by -1 may generate SIGFPE ... blah, blah, blah


There is no more degenerate kind of state than that in which the richest are supposed to be the best.
Cicero, The Republic
bingo by i (2.00 / 0) #8 Fri Dec 22, 2006 at 05:18:42 AM EST
get a cookie (of the non-evil kind)


[ Parent ]
One reason by anonimouse (2.00 / 0) #6 Fri Dec 22, 2006 at 05:11:24 AM EST
Something divided by zero isn't necessarily zero; it tends to +/- infinity. Returning zero instead of handling the exception or ensuring the exception does not occur in the first place is bad practice.

Girls come and go but a mortgage is for 25 years -- JtL
the function is nonsensical by i (2.00 / 0) #9 Fri Dec 22, 2006 at 05:20:24 AM EST
i could throw an exception instead of returning 0, but that's not the point.


[ Parent ]
Not in integer maths. by ambrosen (2.00 / 0) #12 Fri Dec 22, 2006 at 05:22:52 AM EST
Undefined is a far better result to give. And what with no built in exception throwing, 0's the best invalid result available. Still, the essence of the question is whether 0's the only divisor that can cause an error. The answer is not.

[ Parent ]
thanks everyone who congratulated me with a new job | 15 comments (15 topical, 0 hidden) | Trackback