setjmp

Is it allowed to do longjmp() multiple times for one setjmp() call?

為{幸葍}努か 提交于 2021-02-10 03:26:14
问题 In my understanding, a typical usage of setjmp() and longjmp() is exception handling (usage in libpng should be a famous example of that) and there will be at most one call of longjmp() for one setjmp() call. Is it safely allowed to do longjmp() multiple times for one setjmp() call like this? #include <stdio.h> #include <setjmp.h> jmp_buf jb; int i; int main(void) { i = 0; setjmp(jb); printf("%d\n", i); i++; if (i < 10) longjmp(jb, 1); return 0; } Output: 0 1 2 3 4 5 6 7 8 9 I successfully

Is it allowed to do longjmp() multiple times for one setjmp() call?

╄→尐↘猪︶ㄣ 提交于 2021-02-10 03:23:32
问题 In my understanding, a typical usage of setjmp() and longjmp() is exception handling (usage in libpng should be a famous example of that) and there will be at most one call of longjmp() for one setjmp() call. Is it safely allowed to do longjmp() multiple times for one setjmp() call like this? #include <stdio.h> #include <setjmp.h> jmp_buf jb; int i; int main(void) { i = 0; setjmp(jb); printf("%d\n", i); i++; if (i < 10) longjmp(jb, 1); return 0; } Output: 0 1 2 3 4 5 6 7 8 9 I successfully

Why a segmentation fault occurs calling a function inside setjmp()?

倾然丶 夕夏残阳落幕 提交于 2020-01-05 03:55:08
问题 I do not understand why in the function middleFunc() , a segmentation fault is raisen when entry_point(arg) is invoked inside the if ( setjmp(middle) ) statement. #include <stdio.h> #include <setjmp.h> jmp_buf start,middle,end; void finalFunc(void *v) { printf("hello\n"); return ; } void middleFunc(void (*entry_point)(void *), void *arg) { //just debug : this does not cause segmentation fault entry_point(arg); if ( setjmp(middle) ){ //this casues the segmentation fault entry_point(arg); /

Use of setjmp and longjmp in C when linking to C++ libraries

拥有回忆 提交于 2019-12-23 09:17:47
问题 I would like to use setjmp and longjmp in a C program that links to a library that is implemented in C++ (but has a C API). The C++ code does do dynamic memory allocation and pointers get passed through the API, but as long as the C side of the code manages those (opaque) objects correctly, there shouldn't be any messing up when using longjmp, right? I know it's not safe to use these functions in C++ code, but should it be safe in C code that is linked to C++ code? 回答1: The fact that you call

Implementing setjmp and longjmp in C without built in functions or assembly (getting incorrect return values)

泄露秘密 提交于 2019-12-22 06:58:04
问题 I'm trying to test 2 of my functions that sort of mimic setjmp and longjmp for a homework - which is pretty difficult since we're not allowed to use built in functions or assembly asm() to implement the longjmp and setjmp functions. (Yes, that's really the assignment.) Problem : I keep getting wrong return values. So, in short, when main() calls foo() and foo() calls bar(), and bar() calls longjump(), then bar() should not return to foo() but instead setjmp() should return to main with return

About setjmp/longjmp

六月ゝ 毕业季﹏ 提交于 2019-12-20 10:37:57
问题 I was investigating setjmp/longjmp and found out that setjmp saves registers such as instruction pointer, stack pointer etc... However what I don't get here is that, can't the data in the stack of the thread itself be modified between the call to setjmp and longjmp . In that case, wouldn't longjmp not work as expected. To make it clear, for example, when longjmp restores the stack pointer, say the data in the memory the stack pointer is pointing now is not the same as was when setjmp was

Why volatile works for setjmp/longjmp

主宰稳场 提交于 2019-12-17 22:46:59
问题 After invoking longjmp(), non-volatile-qualified local objects should not be accessed if their values could have changed since the invocation of setjmp(). Their value in this case is considered indeterminate, and accessing them is undefined behavior. Now my question is why volatile works in this situation? Wouldn't change in that volatile variable still fail the longjmp? For example, how longjmp will work correctly in the example given below? When the code get backs to setjmp after longjmp,

Use jmp_buf in multiple file

佐手、 提交于 2019-12-13 09:10:37
问题 For clearly, please view my sample I have two files: main.cpp and myfunction.h This is main.cpp #include <setjmp.h> #include <myfunction.h> int main() { if ( ! setjmp(bufJum) ) { printf("1"); func(); } else { printf("2"); } return 0; } This is myfunction.h #include <setjmp.h> static jmp_buf bufJum; int func(){ longjum(bufJum, 1); } Now, I want my screen print "1" and then print "2", but this code is uncorrect! Please, help me! Thank you so much! 回答1: If you want to have it in multiple files,

Communication protocol and local loopback using setjmp / longjmp

穿精又带淫゛_ 提交于 2019-12-12 16:24:52
问题 I've coded some relatively simple communication protocol using shared memory, and shared mutexes. But then I wanted to expand support to communicate between two .dll's having different run-time in use. It's quite obvious that if you have some std::vector<__int64> and two dll's - one vs2010, one vs2015 - they won't work politely with each other. Then I've thought - why I cannot serialize in ipc manner structure on one side and de-serialize it on another - then vs run-times will work smoothly

Setjmp/longjmp in Ruby’s Continuation

左心房为你撑大大i 提交于 2019-12-11 07:28:32
问题 I was wondering about this while digging through the code of cont.c in Ruby’s current version. The documentation of setjmp says that calling longjmp on the jmp_buf structure after the caller of setjmp returned is an error. But, it seems that Ruby does this happily and does not crash: https://github.com/ruby/ruby/blob/05f087c844f0e1e7cfe323edcf591de64069a289/cont.c#L522 https://github.com/ruby/ruby/blob/05f087c844f0e1e7cfe323edcf591de64069a289/cont.c#L775 (+ a couple more usages with fibers)