我在多线程 C 应用程序中发现了这个。作者评论说,它用于在自定义断言函数中导致线程崩溃。 GCC 没有问题,但 clang 发出以下警告:
note: consider using __builtin_trap() or qualifying pointer with 'volatile'
并且还针对断言函数的每次使用发出其中一个:
warning: indirection of non-volatile null pointer will be deleted, not trap
这是怎么回事? __builtin_trap 是特定于 clang 的吗?我应该使用它吗?
最佳答案
写入 NULL
地址并不能保证可靠地使您的程序崩溃,因此 GCC introduced __builtin_trap
为此。
看起来 clang 决定更进一步,完全消除此类写入,几乎迫使您使用 __builtin_trap
。与 __builtin_trap
相比,他们将 NULL
转换为 volatile
指针的其他选择看起来并不吸引人,因为它“仅仅是”未定义的行为。
关于使用 *(int*)NULL = 1 使线程崩溃;有问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10153246/