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

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

问题描述

在linux / gcc上的C ++中使用longjmp和setjmp可以安全地使用下面的语句?


  1. 异常处理我不知道longjmp / setjmp会对标准异常处理产生什么副作用)

  2. * this 指针

  3. 信号

  4. 智能指针(boost的共享和入侵指针)

  5. 可以想到。


解决方案

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

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


自动对象将被抛出的异常转移
控制到程序中的另一个(目标)点,然后调用 longjmp(jbuf,val)


因此,底线是 setjmp() / longjmp()在C ++中不能播放。


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

  1. Exception handling (I'm not implementing exception handling using longjmp/setjmp. I want to know what side effects longjmp/setjmp will have on standard exception handling)
  2. *this pointer
  3. Signals
  4. Smart pointers (boost's shared and intrusive pointers)
  5. Anything else you can think of.

解决方案

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

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

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.

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

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

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