I have the following function implementation that checks for NULL pointer. This is a part of Linux driver, C code:
bool is_null_ponter(volatile void* ptr)
{
return (NULL == ptr)? true : false;
}
What is the advantage of volatile in that case? Why not to put it in a register?
Thank you.
The
volatilehelps that any pointer can be passed to the function without error or warning even if it isvolatilequalified.But this is bogus, the driver you see seems not to be written very professionally.
First, to be complete such a pointer should also be
constqualified, only then it would capture really all pointers.Then, there is no use case for such a function at all. In a
_Boolcontext (here!ptr) pointers naturally lead to the correct answer, this is specified by the standard. Doing it differently as you see here is probably much frowned upon in the kernel community and I guess that this driver has not been reviewed properly.