- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
<font size="2">So they were implementing an accounting system (instead of buying one off the shelf or using an ASP) and in PHP?! And it took over a year?! And still didn't work?!
WFT?! doesn't come close to describing my feelings...
</font>
Admin
MMM was ready to answer then I noticed the very nice use of fallthrough to actually use the default case.
If I'm not mistaken, this code should switch forever between 0 and -1
Admin
No kidding, GL systems must be one of the most widely available to buy off the shelf.
Admin
I'm afraid you're mistaken. If I add a printf statement:
<font size="1">for (int i = 0; i < 10; i++)
{
</font><font size="1"> printf("%d\n", i);</font><font size="1">
switch (i)
{
case 0:
i *= 1;
case 1:
i--;
break;
case 2:
i = 7;
break;
default:
i++;
break;
}
}</font>
then this will print
0
1
1
1
1
1
...
Admin
5) We appear on TDWTF
6) Enjoy the glory
Admin
Stroustrup explains it here:
http://www.research.att.com/~bs/bs_faq2.html#sizeof-empty
Admin
No. Note that the methods all have parameters. They simply do nothing at all.
Admin
Not spellainable, though.
Admin
I'm sure that "main table code" has a deeper meaning that us mere mortals cannot fathom. Don't behave as if you've never dropped a space by accident.
Admin
If they really want to call the "return" function, tell them to write a call wrapper in Assembler.
Admin
In C++, it's actually undefined.
Admin
A conforming C99 compiler (at least) is required to produce a diagnostic and the standard does not specify undefined behavior either explicitely or implicitely.
IANALL (I am not a language lawyer.)
Admin
No, it's not. "sizeof" not accepting incomplete types is an explicit constraint, which means that
a) Violation must produce a diagnostic.
b) Undefined behavior does not follow.
(This post is C99-verified.)
Admin
Same in C. It's actually just this question (well, a very similar one) that gave rise to the expression "nasal demons" - someone pointed out on a newsgroup that upon encountering sizeof() applied to an incomplete type, a C compiler is (implicitly) allowed by the standard to do absoluetely anything, including making demons fly out of your nose.
Admin
By definition you will never meet a black hole at anytime other than midnight. Though by the time you realize that the black hole is turning your local noon into midnight much faster than normal, it is too late to escape.
Admin
Have you actually run this code? For the code to run the way you say, you need a break for case 0. As it is, here is a by-hand trace:
loop control: i=0, termination? not yet, so enter loop
printf(): prints 0
switch case 0: i=0 (0*1 equals 0)
fall through to case 1: i decremented to -1
loop control: i incremented to 0, termination? not yet, so loop continues
Rinse, lather, repeat from printf() on..
Sincerely,
Gene Wirchenko
Admin
Oops... I missed the lack of a break; statement after case 0. You're absolutely right.
If I add another printf() statement after the switch, the output will indeed be
0
-1
0
-1
0
-1
...
Admin
Do you feel bad about implying someone else was a dumbass when, in fact, you were the dumbass?
Sincerely,
Richard Nixon
Admin
Not particularly... missing the lack of a break; statement is a common error...
Admin
And not particularly, since you only suggested the person was mistaken. That is no implication of something worse, just a polite statement. Mine was the same, especially since I was not 100% sure, only ninety-something.
break in switch is one of the things that I very much dislike about C.
Sincerely,
Gene Wirchenko
Admin
Blizzard's World of Warcraft code has this function in it:
function TEXT(string text)
{
<FONT size=+0>return</FONT> text;
}
Admin
It's written in PHP.