gpt4 book ai didi

c++ - setjmp/longjmp 在发布和调试中的不同行为

转载 作者:太空宇宙 更新时间:2023-11-04 01:47:40 26 4
gpt4 key购买 nike

这个问题来自 SetJmp/LongJmp: Why is this throwing a segfault?

当我使用 Debug模式运行代码时,它确实按预期崩溃了。但是如果我使用 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
...

据我理解,longjmp 可以被认为是return 所以b_helper 的堆栈内存将被删除并且访问变得非法。使程序崩溃变得合理。但为什么它在 release 中给出了不同的行为?看起来像 return 表现得像 longjmp。所以发布的结果应该是正确的,我的理解是错误的。

明格:5.3

最佳答案

有问题的代码导致undefined behaviour .该程序不正确,没有预期的行为。您不应期望任何特定的输出或其他行为,也不应感到惊讶。

关于c++ - setjmp/longjmp 在发布和调试中的不同行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50381854/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com