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

发布于 2024-12-10 20:11:37 字数 388 浏览 2 评论 0原文

我目前正在(不要问为什么: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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

谁人与我共长歌 2024-12-17 20:11:37

如果您看到“断言失败”,然后是调试打印,然后是退出,则有两种明显的可能性。

一是断言消息和调试打印将进入两个不同的缓冲输出流(例如 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().

烏雲後面有陽光 2024-12-17 20:11:37

如果您使用的是基于 glibc 的系统,问题可能是 fprintf 在内部调用 malloc,而 assert 又使用 fprintf 打印断言失败消息。这当然是一个非常糟糕的设计,因为从内存不足的情况打印错误消息总是会失败(以及许多其他问题),但事实就是如此......

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. 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...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文