- 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
Okay, I thought about it:
while (*(p++)+=(*p==',')<<1);
Fixed :)
Edit:
while (*p+++=(*p==',')<<1);
I didn't expect that to happen O_O
Admin
Wait, is that guaranteed? There's no sequence point, so I'm skeptical, but I don't know the std well enough to actually say.
Admin
ZWINDOW STARCHILD
Congratulations, you've just named Frank Zappa's next kid.
Admin
You ask either sentinel what the other one would say, then you go through the opposite door.
This works because the liar will lie about what the truth teller would say and the truth teller will tell you what the liar will say, thus, both will tell you the same door: the wrong one.
Admin
Admin
Two words: "sticky registers."
Admin
Admin
But what happens if there are three sentinels, one of which will send SIGKILL to your process if you ask a tricky question?
Admin
Most of your C examples, even if they work, are undefined:
http://c-faq.com/expr/evalorder1.html
Using the post-increment operator and referencing the lvalue incremented elsewhere in the same expression causes undefined behaviour.
Admin
Well, I told you that was including undefined behavior (ie: results are different depending on where you put the ++). A true non portable wtf. ON Visual, you need "while(*p++ +=(*p==',')<<1)", on gcc "while(*p+=(*p++==',')<<1)" [ahah, shorter by one char, which proves the superiority of gcc! :-)]
Btw, I sometimes answered to people asking for their homework to be done in mailing lists newsgroup with workable but evil code (Here for instance ) Maybe I am responsible for some of the wtf you have to maintain...
Admin
Admin
I thought I knew a good bit about C, but I must confess I've never seen *> before.
Is x*>b shorthand for (*x)->b ?? (like how x->b is basically shorthand for (*x).b ) Or is like calling x.operator>(b) ??
I really have no idea what the compiler parses that line to...
Admin
Admin
Its C++ and dynamic_cast<T*>(U*) does a polymorphic cast to T* from U* if T is an appropriate type for the cast, or returns a null pointer if the cast is invalid.
If you're familiar with C#, its behavior is very similar to the 'as' operator.
Admin
Why not abstract the problem into a useful, reusable tool? If such a tool doesn't already exist, of course. ( Sorry, my cstdlib knowledge is a bit rusty )
Admin
I would say that, at least during active development, the second snippet is not necessarily worse than failure -- I have left normally-dead source code in quite a few places where I would drop debugging hooks (and where I got tired of re-writing the almost identical debugging code that I deleted three days before). Maybe I'm giving the developer too much credit, but that is what the switch with an empty default case looked like to me.
Admin
Only because you pussied out and put in a space to divide up the 3 plus signs, it compiles fine without it ;)
I was curious though about how other compilers would deal with though, I followed up on:
And according to this:
http://c-faq.com/expr/evalorder4.html
We should be able to expect far more spectacular results than simply mistaking where the increment goes. I don't anybody has such a compiler to hand?
Admin
Wow!
You win! :D
That is so beautiful I could cry.
captcha: cognac (cuz it tastes good)
Admin
+1 evil This was great. Took me a few to realize you're generating a c app, compiling it and getting the compiler to give you the errors in the data file. (Evil) Genious.
Admin
The switch just does one thing every time, right? Let's replace it with that thing!
Admin
WWhhhat iiis te mattrr wiyht thexee peepll?!!! Ar thy drynking an codkng?!!!
Admin
OMG, you're right. I used to known that the C preprocessor takes the longer match. Maybe my C is getting rusty...
Admin
It's called Duff's device,
got to love it when you INTERLACE controle structures.
Admin
Am I the only one who didn't read the submitters name and looked up Pallas' Law on Wikipedia? I found Marcus Antonius Pallas who "was the source of a law that stated that a free woman who married a slave would remain free if the master approved."
Admin
Snippet from under the rug:
Admin
I used to work with a programmer that honestly believed something like this... he would always nest an if() inside a while() or for() loop - "to be safe - because sometimes it doesn't work otherwise", i.e.
I tried explaining the insanity of doing this, but he thought I was the crazy one...
Admin
Whats the algorithm that you have USED here ..? Its seems to be very complex :P
Admin
hhmmm... Devi giving explanation too to that code :P
Admin
Well, while I can see why this code works (the compiler chooses the evaluate the r-value before the l-value...but it doesn't have too. In fact your code is strictly undefined do to sequence point rules.
Admin
<nitpick>That should be ++p to make it run faster, because you're not doing anything with the value of p in that statement.</nitpick>
Admin
buffer could point to an array of ports, in that case the same address might contain different values each time it is read ...
Admin
why is ++p faster?
Admin
Yeah, I took me a while to work it out. It works because you can change a ',' to a '.' by adding 2 to it. So, if we simplify it and rearrange it a bit it looks like this:
I'm not sure if this is in the standard, but *p==',' resolves to 1 if p is a comma and 0 otherwise. By shifting once left 1 becomes 2, while 0 stays 0. Therefore the code says add 2 to *p if *p==',', otherwise add 0 to it.
Admin
For any sane class that has had the ++ operators overloaded, the meaning of 'p++' means 'increment p, but give me back the previous value', whereas '++p' means 'increment p, and give me back the new value'. Since the latter doesn't require the method to remember the previous value, it's going to be faster.
For integers and pointers, there won't be a speed difference.
Since under normal circumstances ++x will always be at least as fast as x++, experienced C++ programmers will prefer to use the ++x form unless there's a good reason not to. (There is sometimes, usually in a '*x++' style of expression.)
Admin
Well, that used to be the case with naive compilers, because to compute i++, some naive compilers would do:
With the motorola 68k, tables turned, because the processor had built-in support for post increment. So it was easy for compilers to be efficient when generating code like p[i++] = 42;
Then, with C++, the opposite came true again, for classes. For complex types, the implementation of pre-increment can just return a reference to the object itself, while, for post-increment, the implementor of "++' have probably return a copy of itself in the non-incremented state. Hence all stl iterators use ++i, instead of i++.
In the given example, p++ and ++p will be of exact same perfmance with a a reasonable compiler.
captcha: onomatopoeia (wtf?)
Admin
Yes, boolean operators evaluate to 0 or 1, this is in the standard. Of course, that '.'-',' is equal to 2 is not.
captcha: onomatopoeia (again? wtf?)
Admin
void ReplaceCommas (char *buf) { int i,j; len=strlen(buffer);
}
Democracy.
Admin
Frank Zappa is dead since Dec 4th, 1993. So how he is going to have a kid unless he got something stored before his death .... ?
I concur with the name as such, though.
Admin
No, it causes undefined behaviour because without an intervening sequence point, "p" is both written, and accessed for a purpose other than determining the value ot be written.
Admin
Admin
Admin
A WTF that nobody has explicitly mentioned yet: the code wastes time by scanning the string twice. (Once to find the length, and then once to perform the transformation). It would be better to check for the end terminator during the same pass as the transformation.
Also, some people suggested (jokingly) that the extra checks could be in case the variable changes between reads. However this is not possible, as the variable is not declared 'volatile'. Most (if not all) compilers would optimize away all the extra reads and unreachable code.
Finally, the suggested code that relies on ASCII values of ',' and '.' is of course non-portable, as that relation does not hold in EBCDIC and other character sets.
Admin
Admin
There is a lot more to Pallas than that.