C语言:你的程序如何在“assert()”之后继续运行一段时间?失败了?
我目前正在(不要问为什么:P)实现我自己的 malloc() 和 free() 版本,并且出于当前调试目的有意在 free() 的第一行放置了一个断言(0)。
驱动程序正在测试这些 malloc() 和 free() 的随机序列,以测试我的实现的正确性。
然而,当我运行驱动程序时,外壳打印出“断言‘0’失败”,继续运行一段时间,然后打印“中止”。实际上,看起来它甚至可以在报告断言失败和最终报告程序已中止之间多次调用 malloc() 。我确信这一点,因为我在代码中放置了某些 printf 语句来打印某些变量以用于调试目的。
我根本不要求任何关于实现 malloc() 和 free() 的帮助。只是想知道,即使在断言报告失败之后,程序似乎仍继续运行很短的时间(甚至可能调用其他用户定义的函数),这意味着什么。
I am currently (don't ask why :P) implementing my own versions of malloc() and free(), and have intentionally placed an assert(0) at the first line of free() for current debugging purposes.
A driver program is testing a random sequence of these malloc() and free() to test the correctness of my implementations.
When I run the driver, however, the shell prints out that "Assertion '0' failed", keeps running for a little bit longer, and then prints "Aborted". Actually, it looks like it is able to even call malloc() several times between reporting the failure of the assertion and then finally reporting that the program has aborted. I am sure of this because of certain printf statements I have placed in the code to print out certain variables for debugging purposes.
I am not asking for any help at all about implementing malloc() and free(). Would just like to know what is means when it seems that the program continues to run for a short time (even possibly calling other user-defined functions) even after an assertion has been reported to fail.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您看到“断言失败”,然后是调试打印,然后是退出,则有两种明显的可能性。
一是断言消息和调试打印将进入两个不同的缓冲输出流(例如 stderr 和 stdout),这些输出流不会按照它们填充的顺序刷新。
另一个是多个执行线程正在调用 malloc()。
If you're seeing 'assertion failed', followed by debugging prints, followed by an exit, there are two obvious possibilities.
One is that the assertion message and the debugging prints are going into two different buffered output streams (e.g. stderr and stdout) that are not getting flushed in the same order they are filled.
Another is that multiple threads of execution are hitting malloc().
如果您使用的是基于 glibc 的系统,问题可能是
fprintf
在内部调用malloc
,而assert
又使用fprintf
打印断言失败消息。这当然是一个非常糟糕的设计,因为从内存不足的情况打印错误消息总是会失败(以及许多其他问题),但事实就是如此......If you're on a glibc-based system, the issue is probably that
fprintf
callsmalloc
internally, andassert
in turn usesfprintf
to print the assertion failure message. This of course is a very bad design, as printing error messages from out-of-memory conditions will always fail (among many other problems), but that's how it is...