- Feature Articles
- CodeSOD
-
Error'd
- Most Recent Articles
- Nothing Doing
- Home By Another Way
- Coast Star
- Forsooth
- Epic
- The State of the Arts
- Planing ahead
- Too Spicy For My Hat
- 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
Edit Admin
TRWTF is reinventing the
strtok
/strtok_r
wheel.Edit Admin
Yop, it's weird. Nothing added there, no shadow copy, no variable length delimiter, it's straight up the same destructive method.
Edit Admin
There is a subtle difference between strtok() and this code - libc-s strtok() skips empty values between tokens. So if your data is
foo::bar
then strtok() happily finds two values - "foo" and "bar". This code also finds the blank string between them.Edit Admin
TRWTF is C's looping mechanisms that allow things like Duff's device and this while/while monstrosity.
Edit Admin
Most languages would allow this while/while thing. Most languages allow you to write
while(condition);
and don't complain. That's all this is.Admin
Aside from the double while typo, which will be ignored by the compiler anyway, this seems fine.
Edit Admin
strtok()
also only does one iteration of this loop and requires the caller to repeatedly call it, using the heinous implementation of storing global state and having a null pointer input mean to continue processing the same input (hence the need forstrtok_r()
). This implementation also eliminates using global state (good), but it always fully tokenizes the input and doesn't give the caller the option of only tokenizing it once or a few times (which may be fine for this particular application).Admin
But but but it's portable!
Edit Admin
strtok()
is often used in loops similar to this one.Edit Admin
It's not guaranteed to be ignored, especially if you've disabled optimisations. It won't generate much codecitation needed, but it won't necessarily generate none at all. (If optimisations are disabled or if the condition of the two while()s involves a function call, it must evaluate the second while()'s condition once, or the presence of the optimiser can be detected.)
(1) I did work with a compiler once (in 1992) that, in its most "aggressive" optimisation mode, when presented with a
while(1)
deliberately infinite loop, generated code to load a 1 into a register, then compare it to zero, and only branch back to the beginning of the loop if the 1 didn't match the 0...Admin
Case fall-through is a deliberate feature of C, not an oversight. When used as intended it avoids needless duplication of code, which is not "monstrous", but beautiful.
Admin
So you end up with something like a string array except you can't tell where the end is?
Admin
"Interesting topic! String in the colon is a serious issue that requires medical attention. It’s crucial to address such concerns with proper care and professional advice. Thanks for bringing awareness to this topic. Do you have any additional information or resources for those dealing with similar issues?"
Edit Admin
right up until you have to maintain it
Edit Admin
Among other WTFs, the
thing is just nasty and requires you to know far too much about the language.
is harder to type but much much easier to read.
Admin
Nothing helps with maintaining code as much as having less to maintain.
Are you really going to claim that C demands knowing more syntax than other industry languages? Really?
Admin
Fall-through in
case
statements is fine in principle; it just should be explicit. Imagine a setup where fall-through is triggered bycontinue
, and it's a compile-time error for any code path in a (non-empty) label to reach the start of the next label. You can still abuse it, if you really want to, but I feel like that's on you (as opposed to the current setup, where it's easy to do the wrong thing by accident).Admin
If you explicitly tell the compiler not to ignore it, then it won’t ignore it, but then TRWTF would be releasing unoptimised builds. And I wouldn’t have said the compiler would ignore the additional while condition if it contained code that couldn’t be ignored.
In practice, every compiler I have immediate access to (back to 1998’s Visual C/C++ 6.0) when presented with this specific code, ignores the additional while clause unless I explicitly /Od to tell it not to.
Edit Admin
"ignores" is the wrong word. It must not actually ignore it. It's allowed to generate no code if there are no side effects, but it has to inspect it to see if there are actual or potential side-effects. (Or use of
volatile
variables...)Admin
I’m happy that my use of “ignore” accurately and succinctly conveyed my meaning. If it didn’t for you, then I’m also happy for you to parse my comments and replace the use of that shorthand with whichever construction of the concept “generates no output” you find appropriate.
Edit Admin
I'm reminded of how BASIC on e.g. the Commodore 64 turned out to leave the program code in a munged state after a certain int-overflow situation, because it destructively modified a string within the program code (and then crashed out before undoing the damage).