3 ms·
Yes, this is pretty much how it is being used in C code. The other acceptable use-case is breaking out of nested loops. There's also `setjmp()`/`longjmp()` but
by mikevm 5y ago
Yes, this is pretty much how it is being used in C code. The other acceptable use-case is breaking out of nested loops.
There's also `setjmp()`/`longjmp()` but that's even more rarely used.
- einpoklum 5y agolongjmp() is the C-language version of going-to labels in other functions. Its basic "use" is not having to unwind the stack; but moreover, it lets you avoid cleanup and resource de-allocation. Usually it doesn't make sense to do that (you get memory leaks), but if, say, your memory allocations happen in some kind of arena - you can just leave it in any junk state and then proceed to clear it altogether after your setjmp() instruction.
- SAI_Peregrinus 5y agoThe most common use of setjmp/longjmp I've seen (in embedded development) is to have coroutines in C. The original use AFAIK was usually to implement exceptions of some variety. Neither use is that common these days.