在C ++中处理CPU异常 [英] Handling CPU exceptions in C++

查看:137
本文介绍了在C ++中处理CPU异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有跨平台的方法来处理CPU异常(例如分段错误或被零除)?可以说,我需要调用一些潜在的不安全函数(例如,从插件文件中调用),这些函数可能导致段错误或执行之前无法测试的其他一些问题。我知道C标准库具有信号处理功能,但是我不知道如何使用它们来处理问题以避免程序终止(我想,我不能只跳转到有问题的函数执行之前的位置) ,还是可以?)。
在Windows下,我可以使用SEH异常处理程序,但是在Linux或任何其他操作系统下,则不能这样做。使用我自己的异常处理程序来处理这些问题该怎么办,Windows / Linux之间有多少不同?

is there a cross-platform way to handle the CPU exceptions like segmentation faults, or division by zero? Lets say, I need to call some potentially unsafe functions (for example from a plug-in file), that can cause a segfault, or some other problems that I cannot test before I execute it. I know, that the C standard library has signal handling functions, but I don't know how to use them to handle the problem to avoid the program termination (I guess, I can't just jump to the location before the problematic functions execution, or can I?). Under windows I could use the SEH exception handlers, but I can't do that under Linux, or any other OS. What about using my own exception handler to handle these problems, how much is that different between Windows/Linux? Would that be even possible (via assembler - lets say just on the x86 platform)?

我主要是出于好奇而问,我不是想解决一个可能的问题(通过汇编程序-可以说是在x86平台上)吗?存在的问题(尚未)。
谢谢

I'm asking mostly out of curiosity, I'm not trying to solve an existing problem (yet). Thanks

推荐答案

libsigsegv 是用于处理分段错误和堆栈溢出的跨平台库。但是,在大多数情况下,当您检测到分段错误时,正确的做法是尽快终止执行,而不是尝试从中恢复。段故障通常表示错误或内存损坏,一旦您损坏了内存,从中恢复几乎是不可能的。

libsigsegv is a cross-platform library for handling segmentation faults and stack overflows. However, in the vast majority of cases, when you detect a segmentation fault, the right thing to do is to terminate execution as fast as possible instead of trying to recover from it. A segfault is usually indicative of a bug or corrupted memory, and once you have corrupted memory, it's virtually impossible to recover from that.

这篇关于在C ++中处理CPU异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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