与崩溃*为(int *)NULL = 1线程;有问题? [英] Crashing threads with *(int*)NULL = 1; problematic?
问题描述
我在一个多线程的C应用程序中发现这一点。作者评论说,它是用来使一个自定义的断言功能的线程崩溃。 GCC是与它的罚款,但铛发出如下警告:
I found this in a multi-threaded c application. The authors commented that it's used to make a thread crash in a custom assert function. GCC is fine with it, but clang issues the following warning:
note: consider using __builtin_trap() or qualifying pointer with 'volatile'
和问题也是其中之一,对于断言函数的每个用法:
and also issues one of those, for each usage of the assert function:
warning: indirection of non-volatile null pointer will be deleted, not trap
这是怎么回事?是__builtin_trap具体到铛?我应该使用它吗?
What is going on here? Is __builtin_trap specific to clang? Should I use it?
推荐答案
写 NULL
地址是不能保证可靠的程序崩溃,这样的 GCC出台 __ builtin_trap
为
Writing to NULL
address is not guaranteed to crash your program reliably, so GCC introduced __builtin_trap
for that.
它看起来像铛决定走得更远,干脆和消除这种写入,几乎迫使你进入使用 __ builtin_trap
。铸造 NULL
到挥发性
指针相比 __ builtin_trap 看起来并不吸引人的其他选择code>,因为它仅仅是不确定的行为。
It looks like clang decided to go further, and eliminate such writes altogether, almost forcing you into using __builtin_trap
. Their other option of casting NULL
to volatile
pointer does not look attractive compared to __builtin_trap
, because it's "merely" an undefined behavior.
这篇关于与崩溃*为(int *)NULL = 1线程;有问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!