- 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
I don't know what kind of self-respecting C programmer uses double-parentheses ((when single will do))
Admin
I think there's a secret message hidden in that code...
Admin
This is one of the times I actually said WTF out loud (well, the full phrase).
Admin
<font color="#000099">for</font> (i=0,j=0;i<strlen (buff);i++,j="" color="#000099"><font {="">switch</font>(buff[i]) {
case <font color="#990000">'+'</font>:
value[j] = <font color="#990000">' '</font>;
buff[i] = <font color="#990000">' '</font>;
break;
case <font color="#990000">'%'</font>:
You can't nest <font color="#0000ff"><font face="Courier New">case</font> <font color="#000000">statements in a <font color="#0000ff"><font face="Courier New">for</font> <font color="#000000">loop.</font></font></font></font>
</strlen>
Admin
hahahhhahaha!!!! BLIND!
thanks for reminding me how much i hate C. last time i really hated it was when i had to convert a C funtion to vb.net, and there were no comments at all!!!!!! kill this language now!!!! move over to vb!!!!!!!! (btw if u could use copper wires it would probably make it much easier!!!)
Admin
BTW well done bro, this is the funniest site i've ever seen, been coming home and checking my rss for the new WTF's! some ppl are soooo stupid! anyways, tnx for the free laughs!
Admin
There's a problem in the HTML. View source and you'll see that it's supposed to say:
etc
Admin
oy! it gets worse:
for (i=0,j=0;i<strlen(buff);i++,j++)
strlen is O(N) for the length of the buffer, effectively turning this into an O(N^2) algorithm. I'd say it's C written with a VB accent.
Admin
Of course you can't, but I expect someone would know that '<' is the open tag for html elements so all text after that point until the next tag hasn't been displayed :)
for(i=0,j=0;i<STRLEN(BUFF);I++,J++)
switch
Admin
begin(pedantry):
seems Alex got a little overexcited when posting.. and stated that this was an "ASCII to HEX function".. and unless I'm missing something it is indeed quite the opposite
end
Although I must say these things lose some of their impact when they repetitive bits are snipped out :) still a good laugh.
And just to add some balance to all these silly comments about C being evil, or VB being evil for that matter: Get over it, on both/all sides. Languages for problems, any sufficiently stupid person can write utterly horrific code in any language. The worst cases in some languages are perhaps wors than those in others, just as the most elegent solution for a given problem in one language might be more elegent than the same in another. (or the most efficient, or short, or readable, etc)
(Don't have an account, but I'm David)
Admin
I hope he gets paid per line of code. Or wait. I don't. Oh nevermind.
Admin
WTF! A VB programmer coding in C! ;)
Admin
Yeah, this totally looks like a VB programmer trying to write in C.
Admin
I am disturbed by seeing ugly stuff like this in the beautiful C-language.
Maybe this kind of stuff should be censored when it's in C, and only shown when written in VB (or BASIC V2.0?), so you won't be responsible for making people like me depressed. I feel this WTF is intrusive, and i feel that my personal life and even my will to live has been compromised.
It is unresposible to publish stuff like this on an open forum, and the poster clearly have not considered the consequenses it could have. To prevent any serious injury in the future (maybe some people will kill themself when seeing this), i'm planning to sue the owner of this website.
Admin
ah, but when you use the correct compiler and the correct optimizer flags and pray to the correct deity, this code actually compiles to the fastest, most optimized, least memory hungry binary.
Admin
The full file isn't quite the full file. It's just the section that's posted. Even though I'm sure I could reconstruct it, I want to see it for myself!
Talk about your massively unrolled loop.
Admin
Please now, let us not condem an entire programming language because some idiot doesn't know how to use it.
Here is a more reasonable and concise "hard core" bit of 'C' code to ponder...
If you're really "hard core", you'll probably recognize the following code snippet as Duff's Device. Otherwise, you'll probably not be able to figure out what it does, or how.
register n = (count + 7) / 8; /* count > 0 assumed */
switch (count % 8)
{
case 0: do { *to = *from++;
case 7: *to = *from++;
case 6: *to = *from++;
case 5: *to = *from++;
case 4: *to = *from++;
case 3: *to = *from++;
case 2: *to = *from++;
case 1: *to = *from++;
} while (--n > 0);
}
Admin
Haha. *sob* I would advise you to take a class in assembly language, to avoid becoming a WTF yourself in the future. ;p
It used to be that with a well-written C program you could compile down to vector jumps with switch statements and judicious use of registers. To be honest I don't know how it would be done in these days of huge caches and long pipelines, but what I have seen is usually just extrapolations on the theme.
Not... this. Eek.
I take your point that a compiler could refactor this whole pile into one tiny, tight routine. Except that compiler doesn't exist, and if it did we'd all write shitty code because the compiler'd always clean it up for us. It will, a little, but this is whole levels of shitty beyond that.
Admin
I have to agree with David (get an account!! [:)]). I have used both C (and C++ for that matter) and VB (and VB.Net too), and both have their charms.
If I need something really quickly I will use VB.Net, as it's less likely to break with fast, unverified programming.
I will also be trying some C# this year I think, just to see how that flows.
Drak
Admin
C# flows very nicely. I used to favour C++, but now i get the convenience of C# (which is about as convenient as VB.Net). When you need to do something more "traditional" (eg: some fun pointer arithmetic to piss off some auditors - modulus based ring buffers are easily hidden) you can use unchecked { ... } blocks for allowing overflow, and unsafe { ... } blocks for use of pointers and other "fun".
Theres also some other classes and statements for dealing with pointers safely in normal code. unsafe/unchecked blocks require full security permission to execute (for obvious reasons).
Also my C# tip of 2004: The sealed modifier is used to prevent inheritance of a class. What is undocumented is that sealed can also be used as a modifier to a function, preventing overrides of just that function.
Admin
What's the point? It seems to me that the effect can be archieved in more readable and as fast/faster code.
Admin
wow, VB coder with a chip on his shoulder. [8-|]
Admin
UNRESPONSIBLE? What kind of word is that? Irresponsible please! That's the problem with you C-type programmers, lack of communication skills.
A humble VB programmer with a penchant for [the English] language.
Admin
hehe... vb and c, good for various things, but usually crap if you are trying to do the same thing.
i stick to C/C++, because I feel it looks a bit better on the resume and if the topic ever arised, I would rather impress an interviewer with knowledge of something OOD.
but nowadays, the gap is being bridged, 'specially with VB.NET and C#... is there any difference? they are both compiled into MS IL anyways, just a difference coding preference
but you would have to admit, C++ is slightly better than VB, although the difference is not great in many respects, but its often harder to learn to be a good C coder/
Admin
Would the following C++ crap be considered a more concise and ugly method? I'm hoping it works, and assuming in, out are char* and len is the length of in. I'm even nice and allocating the output buffer :D
Notes:
('A'+c-9) is too much code, so i use (56+c)
Admin
<FONT style="BACKGROUND-COLOR: #efefef">Spot the extra bloody colon.</FONT>
Admin
Don't forget that a lot of ppl that visit this site don't have English as their native language (i.e. me [:)])
Anyway - nice piece of code... Guy really should learn:
1) What a string is in C
2) What "hex" is
3) What "ascii" is
4) What "binary" is
5) What "strcat" & "strncat" do
6) What pointers are
I am really amazed that he already knows the "safe" strncpy function, although he still uses the normal strcpy sometimes... (and no - this is no defending of the code [:D])
For the ppl that say the compiler can optimize this -ERR- wrong [:)] strcmp is a function from some library, the C compiler doesn't know what it does, so it can't optimize string comparisons - it's not a language construct as in many other languages.
This site should really organize a contest or smth - rewrite the pieces of code published here in the most optimal way in the same language... Would be fun too see the results... [:D]At least C-compilers are smart enough to generate code that stops evaluating all conditions when || (logical OR) is used and a "true" condition is met somewhere in it (VB doesn't do this, it always evaluates everything) - but still, every loop where a '%' is encountered will trigger a _LOT_ of strcmp's.
PS: The formatting in of these replies really sucks... Half-my-ass HTML and some wannabe BB-code? Wrote a nice parser for the latter - but well - it's C eeh... I mean PHP [:D] [;)]
Admin
This is just so delightful. strncpy(strg, " Copyright ", 11) would have been so much more concise, and much more readable.
I'm not even going to comment on the rest. It's just too painfully obvious that this isn't a native C coder.
Admin
<FONT size=2>
</FONT>Since it's part of the standard C library, the compiler is allowed to know what strcmp() does. And many compilers will actually replace the function call with the actual underlying code - for example, VC++ 6 will, if run with /Oi, inline strcmp().
<FONT size=2>
Ah, yes. I actually fire up IE for this forum, since the handling under any normal browser is so bad. When place like LiveJournal can get formatting right, and actually understand threading, using phpBB is a bigger WTF than any code yet posted.
Admin
This would not be correct... The ('A'+c-9) should at least be ('A' + c - 10), and the compiler optimizes this anyway - so no need to make your code less clear :) Also, you start with the wrong nibble, and you don't shift it with 4 bytes...
Code below works (both way algorithms)
[code language="c#"]
[/code]
Admin
I left the best out: After posting the code to Alex i found this code right after the hex to ascii conversion:
<FONT face="Courier New">out = popen(SENDMAIL_PATH,"w");
fprintf(out,"From: xxxx\n");
fprintf(out,"To: xxxxx\n");
fprintf(out,"Subject: Feedback-Error\n\n");
fprintf(out,"Hexcode [%s] ist dem Programm nicht bekannt!\n\n",hexstr);
pclose(out);
kontaktformular = "nein";
show_error("../../fehler/feedback/index.html","<br>Es ist ein Fehler aufgetreten und direkt an unsere Technik weitergeleitet worden!<br><br>Bitte versuchen Sie es später noch einmal.<br>");
exit(1);</FONT>
Admin
Indeed. However, trying to optimise round a function call is extremely difficult. If the code can pull the content of the function call inline, then it can (not necessarily will) be able to do a much better job. It need no longer push registers that the function never overwrote anyway. Register colouring can work. If it works really, [li] really hard, it might throw away stuff that turns out not to be required.
(Though I think it's on a hiding to nothing with this code.)
As far as the strings thing is concerned - well, all the compariosons are against short fixed strings. There is a chance that the intrinsic code could actually just do two byte comparisons rather than a loop.
Still, the original code is a total mess. I'd recommend the author look at 'Unicode Demystified' . It contains code that addresses much more complex requirements, with much simpler code, and it might inspire him to write C, rather than VB using C syntax.
Admin
Just because some doofus managed to convert his VB script into compilable C, it doesn't make him a C programmer. [:P]
Dead giveaway: "j = j + 2;"? wtf? That's like wearing women's underwear. [:$]
Anyway, I think the maximum wtf density is attained in the 256-clause if statement that is a tautology. [:@]
I can't stand to read any more. [+o(]
Admin
Probably the dumbest part...
I can see myself debugging that for hours and not noticing too ;)
Admin
Agreed ...
Well, this all depends on how you define "hard core". I presume Duff's Device is one of those old C chestnuts that a certain breed of C programmer takes great pleasure in discussing? That must make me not "hard core". Oh well.
Speak for yourself (whoever you are, "anonymous"). I agree that it looks confusing, but it doesn't take that long to work out. If this code compiles, then this must mean that the switch/case statements aren't treated by a C compiler as a construct in the same way the do ... while is. The do ... while compiles independently of the switch/case, and the switch/case is then interpreted as a jump into a particular position in that compiled code - this makes sense, given the default fall through behaviour of C. The switch statement is executed only once, meaning that the first run through the loop is only partial. Given that the address of the destination (to) isn't incremented in the way that the address of the source (from) is, it is probably some kind of serial device. So the net result is that count values are copied from memory starting at location from, to a serial device of some description. I presume they are copied in blocks of eight (first block may be less than eight to give exactly count values overall) for performance reasons?
Admin
Admin
grr, fucking piece of shit forum software.
This isn't phpBB. phpBB doesn't suck NEARLY this bad. Actually I rather like phpBB, after making a few simple modifications to it anyway.
Anyway, this is written in ASP, not PHP. That should be your first clue that this isn't phpBB.
Admin
Actually its in ASPX. The .Net variant of ASP. Which in itself means nothing as the editing control is javascript/vbscript I guess.
Drak
Admin
The reason most bad code is in Visual Basic, is because most of all code is in Visual Basic.
"To the first approximation" all programmers are VB programmers. All other languages are in the noise level (or as they put in on "The West Wing" last week "within the margin of error...of not having any support at all")
Admin
Booger...Anonymous again. That last one was me.
Isn't this site supposed to auto-logon?
Admin
Get the IEView extension for Firefox. It will save you a ton of frustration with this forum. You only need to use IE if you're quoting though. [:)]
Admin
Ah, thanks for the clarification. I'd ended up trying to find out what the heck BB was (it's very briefly mentioned, with no explanation in the site FAQ) and arrived at the phpBB pages. I then made the assumption that this was using that.
I have a friend who works for a certain very large company that happen to run, among other things, a certain two web applications.. One is driven by Python. The other by ASP. One is incredibly successful, and always responsive. The other is godawful slow. They're looking to rewrite the ASP one into Python.
And yes, you probably use one of them frequently.
Admin
DavidM, you're on target about Duff's Device. Efficiency is why it does the "blocks of eight" thing with the mod-eight part done first: That way, it only has to check to see if it's done on every eighth byte.
The best part about the strncpy() issue is that in the two places where he should be using strncpy() but isn't, he is using strncpy() for essentially the same thing, on the line immediately preceeding the horrible unrolled gunk.
Between that, and "i = i + 2", and a few other items (not least the 256 case sensitive strcmp()s instead of two isxdigit()s), and strcmp()ing "25" etc. when he should've strtol()'d the string once and switched on the integer) -- this poor guy is not a C programmer.
Admin
That's a wild assumption. Could be that everything this person ever did was in C, and that all the training he/she ever got was from one of those "teach yourself C in an afternoon" books. This snippet could as easily be a small sampling of hundreds of megabytes of equally-bad source code.
Not a competent C programmer? No. Not a good C programmer? Definitely not. Not a C programmer? Not enough data to draw a conclusion.
Admin
You can add 'Not a competent programmer' to that list, and leave out the C. Anyone with some feeling and competence for/in programming could think of a better way to do this, regardless of the language they program in.
Drak
Admin
The language has nothing to do with crappy code.
Admin
You can. You may not want to - but you can:
Compiles and works just fine. It's the same as Duff's device mentioned elsewhere just - probably - not as useful. Remember that a switch{ case: } construct is just a structured "goto" in C.
Admin
Duff's Device
Basically, it's loop unrolling (as done by any optimising compiler), which means the repeat condition gets checked less often, and there are less code jumps for the same number of iterations. It only works if you know how many times it's looping beforehand, but it can make a big difference if the repeated operation is small and done a lot of times (as in here).
Admin
It's just badly posted, look at the C source at the end of the post to see the correct version.
It seems this code attempts to change some string into a verbal form of some sort
Admin
ArmchairDissident, I think that's not what he meant. There was a left
angle bracket in the for loop header, which resulted in the following:
Notice
where it says "iswitch" -- that "i" should be followed by a less-than,
and then the rest of the line. The HTML formatting script screwed up. Among
other things, the for's close-paren and opening curly brace are
missing, as well as a newline. In the original code the switch was not inside the for-loop header. But that reader
mistakenly thought otherwie.s
You are, of course, correct about arbitrary case placement and Duff's Device.
Allow me to say, by the way, that this commenting software is
staggeringly, mind-numbingly bad. It's ass to use, it enforces bizarre,
difficult-to-read font choices, and it fills the comments page with
meaningless and distracting visual noise. The person or persons who
designed it were criminally incompetent. No slightest thought was given
to usability or utility. There is more to good interface design than
spraying brightly-colored crap around the screen.