- 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
Them bullet holes is for speed.
Admin
(*) - non-sequitur, since you ask.
Admin
Admin
That's honestly the only time I would ever use them. In all other circumstances, I think I would find another solution, even if that other solution is slower, takes more time to write, etc. And if that other solution is using a language that has native support for exceptions, then that's fine with me.
Admin
Admin
Grow down already you sad old man.
Admin
I once worked on a project where goto was banned but setjmp / longjmp was fine (yes I checked with the project manager). This was on a project where the cost of failure could be very high (we were subjecting critical aircraft components to simulated usage - if we broke the component all components would have to be replaced when they got to the simulated age of the test piece and we were only just ahead of many in service components).
Admin
Admin
Instead of throwing random insults at people you don't know, could you try to give us some insight into the mindset of the Real Guru (tm) as you claim to be? Just a little beyond 'read teh man page u noob' would be a big relief to give us entry-level c# babies a glimpse of light in this dark, dark place. Looks like we have a big, fat, smelly troll in our midst..
Admin
This was IIRC built with MSVC6. It's probably been fixed in more recent versions.
Yep, absolutely. It's the only-slightly-less-prehistoric equivalent of GET, as in I just can't even see a program containing getch() without subconciously associating it with the style of late-70s/early-80s home computer programs!Admin
Admin
Admin
Admin
Setjmp and longjmp were often used to implement complex error handling in a similar fashion to try/catch blocks. For something complex. like an interpreter, it was a cleaner, more robust way to implement consistent error handling. Usually, the convention in C was to bury it under macros or function calls.
Setjmp has to save the full "runtime" context (so it can return to it later), so it would always be way way more expensive then a loop or a simple goto. It's very strange that anyone using it would think this was somehow faster than just a simple loop (more work is always more time).
Paul.
Admin
Obviously, they were using longjmp for exception handling, which is cumbersome (at best) with a local goto. If longjmp is restricted to this role, it is relatively safe. And they can avoid unsafe goto's and longjmps by banning non-exception handling uses.
Admin
I increased the loop size by an order of magnitude and got 11 seconds for the longjmp and 6 seconds for the loop.
Admin
looks like giovanni is in for the high jump...
Admin
Also, while is not a function, so it may be misleading to say it takes a bool. It evaluates the expression between its parentheses on each iteration and skips if the result of evaluation is false/0.
Admin
Admin
This actually isn't safe. For example, let's say your signal comes while your program's in the middle of malloc. Once you siglongjmp out, you've corrupted your heap AND left the heap locked - touch malloc or free again and you'll lock up.
The safe way to handle signals in unix is to write to a pipe (using the write() system call, which is safe in a signal handler) that your main loop is looking at. Or on recent linuxes, use signalfd.
Admin
longjmp is a WTF. I really can't understand why people use it. If you need it, ur doin' it rong.
The solution to these problems and more is simple: stop using that poor excuse for a compiler.Admin
Funny, the other day I was just reading about setjmp/longjmp in The Standard C Library by P.J. Plauger:
(emphasis added)Admin
Interesting... Do you happen to know any C idiom that has the opposite effect?
Admin
I've never heard of these commands either. Maybe they're compiler-specific.
Yes, I know I could look it up.
GOTO has its place; if it didn't, it wouldn't be included as a machine-level instruction on every chip ever made. I would say that since 90% of all programmers are crap, it's left out because most people can't be trusted with it.
Case in point is today's example.
Admin
Admin
For fun, google for 'man page'. Be sure to search for images! Hint: To not get fired, maybe do that on someone else's computer...
Admin
excelllllent function this .. added benefit of being self documenting
Admin
Fools! You're using the wrong yardstick!
Longjmp is indeed much faster--at getting to code he!!.
Admin
Admin
Check your wife's browser history.
Admin
Admin
I wonder, is machine-level if...else, for and while etc included?
Admin
Don't be so sure.
Admin
I remember inheriting a project where the main menu did a longjmp() if it didn't recognize the menu choice. For added entertainment value, the setjmp() bookmark wasn't set until after the first successful main menu choice. So, if you happened to hit a bad choice on the first try...
Admin
Wait, what?
I'm not aware of a compiler doing this. It's not that it couldn't be that someone does this, but "almost all"? Could you name some current compilers which do this?
Admin
Oh my god. Geeks everywhere. They actually tested it.
Admin
You must be using a 'girly' or 'womanly' OS.
Admin
I disagree. While there are a very small number of cases where goto could be useful, I always avoided it if it can be accomplished without it. One of the basics of software engineering was that if you structured your code properly, 99% of the time you wouldn't need goto.
Admin
I so love the smell of cargo cult in the afternoon.
What about those programmers who actually write compilers, or library code? Hardly any of the C standard libraries are platform-optimized in any way and few compilers use more than 20% of the instruction set of the processor anyway, excepting some simple RISC machines. There is only one C compiler I know that does any kind of instruction-level vectorizing. With all the rest, guess what, you have to write it in assembler.
Really, it is rather amusing to read comments by people who have never written or even read any assembly code to complain how slow it is, as well as those quoting the holy words of Knuth and obviously skipping the first long word that is outside their own jargon and it just comes out as 'optimization is the root of all evil'.
In this example, yes... and there are many cases where people look at what the compiler made from their code and went 'WTF'.
Trust no one. Not even yourself.
Admin
Besides being useful for exception code, setjmp/longjmp can also be used to emulate the gosub/return pair from basic.
(from memory, so don't laugh at mistakes) Create an array of jmp_buf's, and for a gosub use 'if (setjmp(jmpary[gs++])) {goto sbr;}', mark the top of a subroutine with an appropriate label like 'sbr:;', and for the return use 'longjmp(jmpary[--gs]);'. Best if wrapped up as a set of macros.
Can't you imagine the look of horror in a future maintainers eyes?
Admin
You could try spamming "for(;;){" until penis size overflows
Admin
Admin
The RWTF is writing a business application in C in 2005. There are plenty of higher-level languages that would be much cheaper & safer to develop in.
Also, this global routine called calculate() is hopefully anonymized. Otherwise, that's a terrible name.
Admin
Admin
Actually, where longjmp outperforms looping is when you are looping inside 5 or nested functions and need to dump back to the original stack frame fast. If you had a case where 5 functions down the stack, you needed to start the next loop in the base stack frame, longjmp would out perform
repeat 4 times in 4 more functions and if (func() == SOME_BREAK_DEFINE) continue;
It would be faster because it would not have to break down 5 stack frames and perform 5 ifs. It would just modify the stack once and jump.
Still, this is such a rare occurrence, some languages call them exceptions. :-)
Admin
Well, nobody except for Jackalope about 13 minutes before your post... but that's okay. And yeah, its pretty stupid.
Admin
Four seconds really is a long time. My first year at Google, I recall a Friday meeting where a guy was given an award for decreasing the average latency of a simple search by like 1/10 of a second. They sent him to Hawaii for a week, paid in full.
I recall thinking "0.1 seconds isn't all that much... why would they give an award for such a small decrease?" And then I thought about how many searches were done every second, and thought about the horsepower behind each one.
Four seconds is an eternity in some contexts.
Admin
A philosophical view:
If a programmer optimizes a program for speed and nobody tests it, does it make it faster?
All sorts of rules-of-thumb are great for hypothesizing, but at the end of the day, knowing all the general optimization tricks in the world does you absolutely no good if your first principle when optimizing is not the test your relative performance EVERY TIME or don't consider it "optimizing".
Admin
Dude, there's a test at the top of every for loop. Thus, test and branch.
Admin
I don't feel so bad about using goto to jump into the middle of a while() loop now.