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
What is going on here? Is __builtin_trap specific to clang? Should I use it?
Writing to
NULLaddress is not guaranteed to crash your program reliably, so GCC introduced__builtin_trapfor that.It looks like clang decided to go further, and eliminate such writes altogether, almost forcing you into using
__builtin_trap. Their other option of castingNULLtovolatilepointer does not look attractive compared to__builtin_trap, because it’s “merely” an undefined behavior.