简体   繁体   English

setjmp / longjmp在发行和调试中的不同行为

[英]Different behavior of setjmp/longjmp in release & debug

This question comes from SetJmp/LongJmp: Why is this throwing a segfault? 这个问题来自SetJmp / LongJmp:为什么这会引发段错误?

When I use debug mode run the code it did crash as expect. 当我使用调试模式运行代码时,它确实崩溃了。 But if I use release it will output like this: 但是,如果我使用release,它将输出如下:

1 setjmping a_buf
2 calling b                // loop start
3 entering b_helper
4 longjmping to a_buf
5 longjmping to b_buf
6 returning from b_helper  // loop
2 calling b
3 entering b_helper
4 longjmping to a_buf
5 longjmping to b_buf
6 returning from b_helper
...

As my understanding, longjmp can be considered as return so the stack memory of b_helper will be erased and accessing become illegal. 据我了解, longjmp可以被视为return因此b_helper的堆栈内存将被擦除并且访问将变为非法。 that make the program crash become reasonable. 使程序崩溃变得合理。 But why it gives different behaviour in release ? 但是,为什么它在release时具有不同的行为? Looks like return behave as longjmp . 看起来return表现得像longjmp So the result in release shall be the right and my understanding is wrong. 所以发布的结果应该是正确的,而我的理解是错误的。

Mingw: 5.3 Mingw:5.3

The code in question causes undefined behaviour . 有问题的代码导致未定义的行为 The program is incorrect, there is no expected behaviour. 程序不正确,没有预期的行为。 You should not expect nor be surprised by any particular output or other behaviour. 您不应期望任何特定的输出或其他行为,也不要对此感到惊讶。

Which compiler do you use? 您使用哪个编译器? If you use g++ compiler, ie the programming language is C++, the functions b_helper is inlined into the function b , and the function b is inlined into the function a in the release mode with enabled optimizations. 如果使用g ++编译器,即编程语言是C ++,则在启用了优化的发行模式下,将函数b_helper内联到函数b ,并将函数b内联到函数a中。 In this case there are no more routings left that call setjmp and return. 在这种情况下,将不再有调用setjmp并返回的路由。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM