- 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
Not on a machine with a compiler right now but will that compile?
Admin
Amazingly enough, it compiles and even runs, at least with GCC.
An explanation would be interesting...
Admin
Admin
It treates it as 2 separate loops. Let me re-format it so you seee it.
Admin
Why shouldn't it compile? The second loop is a no-op. It has a null statement; and it's always run zero times, since the first loop only exits once ptr == NULL.
Admin
An explanation would be boring.
But here goes.
Try this analoguous code:
A while-loop can have an empty body, in which case nothing happens on each iteration. A commonly-seen usage of this is in the infinite loop: while(true);
Admin
Move the second while statement to its own line and essentially the code is:
while(path != NULL) { // do some stuff... ... } while(path != NULL) { // Do nothing } Since you have just broken out of the first loop, the condition for breaking out of the second is already met, so happily you don't get stuck forever in the second empty loop.
Admin
Yeah, it's a while loop followed by a one-line while loop. Since the only way out of the first loop is for path==NULL, the second loop does nothing -- it tests once and exits.
Dumb, but mostly harmless.
Other junk:
Is there just a single path in getenv("MODULE_PATH")? or are there multiple paths, like this: c:\foobar;c:\foo;c:\bar If not, then I guess CreateSymbol(path) has to extract the paths by finding the path separator character.
Admin
Duh! Now I understand why some people love Python's syntax. Thank you for the explanation.
Admin
Must...resist...urge...to...obfuscate...
Admin
Wow, now THAT code is a genuine WTF. Ugly. Damn ugly.
Admin
Admin
Seems they had a do-while, and changed it to a normal while without removing the do-while exit condition. Since the code kept working, nobody noticed. More of a whoops than a WTF.
Admin
Admin
Admin
Where did he find a compiler that happen to have an Alzheimer Syndrom?
(or, as the captcha hints, a compiler that drinks too much cognac?)
Admin
It's the sort of Whoops The F. (WTF Pronounced Whuh-Tee-ef? instead of the more pervasive Dubya-Tee-Ef!) that makes you wonder what else they weren't paying attention to. It's healthy to maintain a little paranoia when you run across a few of these in a project. The next WTF might be substantial.
Admin
It's the sort of Whoops The F. (WTF Pronounced Whuh-Tee-ef? instead of the more pervasive Dubya-Tee-Ef!) that makes you wonder what else they weren't paying attention to. It's healthy to maintain a little paranoia when you run across a few of these in a project. The next WTF might be substantial.
Admin
Admin
I'm idly wondering if this used to be a do-while and then got converted.
Admin
Maybe it's intentional, just to remind the coder of the exit condition. Although I guess if that were the case, it would be better to just to put it in a comment.
Admin
Admin
Final proof that all intelligent people have abandoned this site.
Shit, what does that make me ?
Admin
And the quote doesn't appear. Nice.
Admin
Yes it compiles. It is valid. To understand, look at this:
int x=5; while(x) --x;
That is a while loop without a block - just a single statement. Likewise, the statement is optional - or maybe better, an empty statement can be used:
while(true);
That will loop forever.
So the code in the WTF, something like:
while(fp!=NULL) { // do whatever } while(fp!=NULL);
It is the same as:
// first loop while(fp!=NULL) { // do whatever }
// now a second, completely unrelated loop: while(fp!=NULL);
Admin
Ahem:
Admin
Admin
Aaaagggghh!!
The REAL WTF is.. well, I don't need to tell you, I'm sure.
Admin
No it doesn't. Testing shows it processes a path like "/etc:/usr/lib:/whatever" just fine.
Admin
And it can crash also quite easily. strdup does not like a NULL pointer, and getenv returns one, when the given variable is not defined.
Admin
Actually, Java is written in C++, and JNI (with a hint or two to the compiler) lets you make calls to C[++] quite easily, but that wasn't the point!
Admin
Mayhap it is. Perhaps you can. But calling Java from C++ I think is the issue snoofle was talking about.
"System.getProperty"
Admin
Specifically, a continuous feed dot-matrix printer, wooden table, webcam, and an OCR program written in XSLT.
Admin
bows in awe
Admin
Sex?
Admin
Whether that is an improvement or not depends whether code simplicity is more, or less, important than code efficiency.
The original C version makes a single copy of the original string, and iterates through it in place very efficiently.
The Java version will do something like copy the string (twice, likely), parse it, copy each part into a new string object, and shove all of them into an array or list object, which is then iterated over to retrieve each part.
If this was part of a routine that was called very frequently, you'll almost certainly see a very significant difference in performance between the two.
Syntactic simplicity comes at a price...
Admin
Ah, I suppose. I didn't know that ':' was meant as a separator. I thought it was the colon in a Windows/DOS drive spec.
Like this:
#include <stdio.h> #include <stdlib.h>
void RegisterPath(int i) { } int CreateSymbol(const char path) { return printf ("<%s>\n", path); } int _tmain(int argc, _TCHAR argv[]) { char *path = strdup("c:\Hello world"); while (path != NULL) { char *ptr = strchr(path, ':'); if (NULL != ptr) *ptr++ = '\0';
}
And the output is:
<c> <\Hello world>Admin
What a beauty! The WTF is really cute and nice.
CAPTCHA calls that 'tastey'.
Admin
Admin
Not only will it compile, but it will run correctly.
Given that
is valid C & C++, and given that
is a single statement,
is equivalent to
Admin
strtok is best of both worlds, and available in standard C:
Admin
is a single statement, (or shorter:
if happens to be one statement)Admin
I think the worst thing about this one is thinking what happens later... imagine the code in the middle of the loop is a bit longer, then someone comes along and changes the first while() condition (e.g. adding an extra term) - suddenly the "no-op" loop becomes an infinite hang... not pretty.
Admin
Admin
Admin
while - while is actually a possible structure, testing both at the beginning and at the end of the loop (I thought I have seen once at a time a language built with it). In C and C++, it can be simulated by:
DO WHILE (condA) { (pseudocode) } REPEAT WHILE (condB);
while (condA) { if (!condB) break; }
Don Knuth even mentioned it in one of of his papers as a possible practical structure (it was at a time C was still a young language).
Admin
It's possible that the 2nd while statement was meant as a comment to indicate what loop the } was terminating. It's not a terrible habit to get into, especially for students, assuming you remember that it's a comment.
Ex:
int main() {
while (run = true) { switch(condition) { case 1: blah++; case 2: blah--; } //switch(condition) } //while } //int main
Admin
Aside from middle of the block exits, wouldn't:
while(condA && condB)
perform the same function more efficiently?
That Knuth person must not know anything about programming! ;P
(Had to spell "onomatopoeia" twice to post this!!)
Admin
while(condA && condB) is not exactly the same as
while(condA) { blah; blah; } repeat while(condB);
The double while() in our wtf checks one condition at the start of the loop and the other at the end, not both at the same time. So the loop would be executed at least once regardless of the state of the second condition.
Admin
No. Your code checks both conditions at the top of the loop, whereas the given code checks one at the top and one at the bottom. So condA won't be checked the last time through the loop, and obviously any side effects of checking condA won't occur.