Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the advantage of "volatile" in this case?

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.


1 Answers

The volatile helps that any pointer can be passed to the function without error or warning even if it is volatile qualified.

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 const qualified, only then it would capture really all pointers.

Then, there is no use case for such a function at all. In a _Bool context (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.

like image 102
Jens Gustedt Avatar answered Jan 28 '26 20:01

Jens Gustedt