简体   繁体   English

C语言:在“assert()”失败后,程序如何继续运行一段时间?

[英]C Language: How is it possible for your program to continue running for a little bit after an “assert()” has failed?

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. 我目前(不要问为什么:P)实现我自己的malloc()和free()版本,并故意在free()的第一行放置一个断言(0)以用于当前的调试目的。

A driver program is testing a random sequence of these malloc() and free() to test the correctness of my implementations. 驱动程序正在测试这些malloc()和free()的随机序列,以测试我的实现的正确性。

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". 但是,当我运行驱动程序时,shell会打印出“Assertion'0'失败”,继续运行一段时间, 然后打印“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. 实际上,看起来它甚至可以在报告断言失败之间多次调用malloc(),然后最终报告程序已中止。 I am sure of this because of certain printf statements I have placed in the code to print out certain variables for debugging purposes. 我确信这是因为我在代码中放置了某些printf语句以打印出某些变量用于调试目的。

I am not asking for any help at all about implementing malloc() and free(). 我并没有要求任何关于实现malloc()和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. 只想在程序报告失败后,即使程序似乎继续运行一小段时间(甚至可能调用其他用户定义的函数),也只是想知道什么意思。

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 (eg stderr and stdout) that are not getting flushed in the same order they are filled. 一个是断言消息和调试打印进入两个不同的缓冲输出流(例如stderr和stdout),它们没有按照它们被填充的相同顺序刷新。

Another is that multiple threads of execution are hitting malloc(). 另一个是多个执行线程正在命中malloc()。

If you're on a glibc-based system, the issue is probably that fprintf calls malloc internally, and assert in turn uses fprintf to print the assertion failure message. 如果您使用基于glibc的系统,问题可能是fprintf在内部调用malloc ,而assert依次使用fprintf来打印断言失败消息。 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... 这当然是一个非常糟糕的设计,因为来自内存不足情况的打印错误消息总是会失败(在许多其他问题中),但它就是这样......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM