gpt4 book ai didi

C++:使用 longjmp 和 setjmp 安全吗?

转载 作者:IT老高 更新时间:2023-10-28 12:37:46 26 4
gpt4 key购买 nike

在 linux/gcc 上的 C++ 中使用 longjmp 和 setjmp 是否安全?

  1. 异常处理(我没有使用 longjmp/setjmp 实现异常处理。我想知道 longjmp/setjmp 会对标准异常处理产生什么副作用)
  2. *this 指针
  3. 信号
  4. 智能指针(boost 的共享和侵入式指针)
  5. 其他你能想到的。

最佳答案

setjmp()/longjmp()完全颠覆堆栈展开,因此也颠覆了异常处理以及 RAII(一般的析构函数)。

从 18.7/4 标准中的“其他运行时支持”开始:

If any automatic objects would be destroyed by a thrown exception transferring control to another (destination) point in the program, then a call to longjmp(jbuf, val) at the throw point that transfers control to the same (destination) point has undefined behavior.

所以底线是 setjmp()/longjmp() 在 C++ 中表现不佳。

关于C++:使用 longjmp 和 setjmp 安全吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1376085/

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