C++:使用 longjmp 和 setjmp 安全吗? [英] C++: Safe to use longjmp and setjmp?

查看:30
本文介绍了C++:使用 longjmp 和 setjmp 安全吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于以下情况,在 linux/gcc 上使用 C++ 中的 longjmp 和 setjmp 是否安全?

Is it safe to use longjmp and setjmp in C++ on linux/gcc with regards to the following?

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

推荐答案

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

setjmp()/longjmp() completely subvert stack unwinding and therefore exception handling as well as RAII (destructors in general).

来自标准中的 18.7/4其他运行时支持":

From 18.7/4 "Other runtime support" in the standard:

如果任何自动对象会被抛出的异常传输破坏控制到程序中的另一个(目标)点,然后在将控制转移到同一(目标)点的抛出点调用 longjmp(jbuf, val) 具有未定义的行为.

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++ 中表现不佳.

So the bottom line is that setjmp()/longjmp() do not play well in C++.

这篇关于C++:使用 longjmp 和 setjmp 安全吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆