c - 用 C 写我自己的 longjmperror()

标签 c setjmp

我正在查看 longjmp 的手册,在错误部分它是这样说的:

ERRORS

If the contents of the env are corrupted, or correspond to an environment that has already returned, the longjmp() routine calls the routine longjmperror(3). If longjmperror() returns, the program is aborted (see abort(3)). The default version of longjmperror() prints the message ``longjmp botch'' to standard error and returns. User programs wishing to exit more gracefully should write their own versions of longjmperror().

我将如何编写我自己的 longjmperror 版本?据我所知,在 C 中你不能重写函数,我真的需要长跳转,以便在它找不到要跳转到的点时以特定方式退出。

最佳答案

无论如何,在 Mac OS X(10.9.2,Mavericks)上,longjmperror() 的原型(prototype)是:

void longjmperror(void);

您使用该签名编写一个函数。它不能返回(或者更确切地说,如果返回,程序将被 abort()ed)。你在那个函数中做什么是你的事,但请记住,对于要调用的函数来说,事情已经发生了适度的灾难性错误)。它可能会在您的日志文件中记录错误,或者只是在退出前写入更有意义的消息(而不是中止和可能的核心转储)。

您将包含函数的目标文件链接到系统库之前。通常不希望您替换系统功能,但这是您打算覆盖的功能。

关于c - 用 C 写我自己的 longjmperror(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22120405/

相关文章:

c++ - 在 Arduino IDE 中使用正则表达式库时未定义对 `longjmp' 的引用

c - Makefile...创建一个静态库

计算节点值的最大总和并计算给出总和的特定路径[C]

c - 使用trace32读取宏值

c++ - 在 longjmp/croak 之前显式调用析构函数

我可以撤消或删除 atexit 命令吗?

c - 64 位 Windows : longjmp lands in a wrong place

c - 如何使用 scanf(不使用字符串函数)读取字符数组?

c - 为什么#define 一个空的宏?

c - 是否允许为一次 setjmp() 调用多次执行 longjmp()?