#169681 - sgeos - Wed Jul 29, 2009 5:52 pm
When is it a good idea to use longjump()?
#169682 - Dwedit - Wed Jul 29, 2009 6:12 pm
So you can always have a restart or quit function, regardless of what functions have been called.
It's also the "C" way to do exception handling.
_________________
"We are merely sprites that dance at the beck and call of our button pressing overlord."
#169731 - sajiimori - Fri Jul 31, 2009 9:01 pm
Yeah, pretty much for exception handling. For instance, the Lua VM uses it to handle errors. Otherwise it would have to return error codes through many layers of code, which is especially painful when some of those intermediate layers are already using their return values for various purposes.
Exception handling doesn't come up in console/handheld game programming very much because rare problems aren't supposed to occur at all; they're bugs that are supposed to be fixed before shipping. Most other kinds of applications don't have that luxury; there's too many unknowns at runtime.
Just don't let longjmp() pass through C++ code that might have objects on the stack with destructors, because all those destructors will be skipped.
#169733 - sgeos - Fri Jul 31, 2009 9:50 pm
sajiimori wrote: |
Just don't let longjmp() pass through C++ code that might have objects on the stack with destructors, because all those destructors will be skipped. |
I've read that misuse of longjump() can result in memory leaks. I've never actually had to use longjump(), but it seems like a super goto to me.
#169746 - Kyoufu Kawa - Sat Aug 01, 2009 3:49 pm
sgeos wrote: |
I've read that misuse of longjump() can result in memory leaks. I've never actually had to use longjump(), but it seems like a super goto to me. |
Super Goto is right. And besides, there's a whole lot of things in C/C++ that can result in memory leaks. Just use it -right-.
#169748 - Dwedit - Sat Aug 01, 2009 4:51 pm
It's not so much a goto, it's really a "Goto And Move Stack Pointer".
_________________
"We are merely sprites that dance at the beck and call of our button pressing overlord."
#169751 - LOst? - Sat Aug 01, 2009 10:41 pm
sgeos wrote: |
When is it a good idea to use longjump()? |
longjump() is what? Is it a GBA thing or a C/C++ thing? If it is a C thing, then I would like to know all about it ;)
_________________
Exceptions are fun
#169752 - vuurrobin - Sun Aug 02, 2009 12:12 am
it's a c/c++ thing.
http://www.cppreference.com/wiki/c/other/longjmp
is this actually being used in a present day application or something that was only used in the days of old. I can't think of any reason to use this, especially with c++ and exeptions.
#169817 - sajiimori - Wed Aug 05, 2009 2:10 am
Well for one thing, it's way lighter-weight than C++ exceptions. It consumes pretty much no memory, whereas exception support makes a much larger executable.
But you get what you pay for, and longjmp is very basic.
#169821 - hacker013 - Wed Aug 05, 2009 9:44 am
So this just moves you to another place in the code like goto ? Or what are the different between the 2?
_________________
Website / Blog
Let the nds be with you.
#169825 - Dwedit - Wed Aug 05, 2009 1:22 pm
Goto is local to within a function. Longjmp moves the stack pointer, so you can call a bunch of functions, then Longjmp out of them all back to where you started. Just like exception handling, but without calling any destructors.
_________________
"We are merely sprites that dance at the beck and call of our button pressing overlord."
#169829 - sgeos - Wed Aug 05, 2009 3:54 pm
Dwedit wrote: |
Goto is local to within a function. |
And it can move forward.
Dwedit wrote: |
Longjmp moves the stack pointer, so you can call a bunch of functions, then Longjmp out of them all back to where you started. |
Think of something that will rewind out of called functions and return control to a preset point.
Dwedit wrote: |
Just like exception handling, but without calling any destructors. |
Misuse of longjump() can have bad side effects. Control is reset to a previous location, but as I understand it not much else is reset.
#169861 - gmiller - Fri Aug 07, 2009 5:29 pm
As others have said setjump / longjump just reset the stack pointer so it can reset local variables but has no impact on globals or the heap. So any allocation on the heap that the pointers were in in local variables are lost and any globals are not reset. You can long jump out of one function back into another if you want. This can be useful or royally screw you depending on how you use it.