Freeing (vfree-ing) pointer to volatile data-Collection of common programming errors
volatile
seems to be a never ending question of every one. I thought I knew everything about it, but then I encountered this:
So, I have a piece of memory shared between threads and I defined it like this:
volatile type *name;
If it makes you feel better, you can imagine type
is just an int
.
This means I have a pointer (that is not volatile) to some data that are volatile. So, for example when it comes to optimizing, the compiler can cache the value of name
but not name[0]
. Am I right?
So, now I am vfree
ing this pointer (it’s in a Linux kernel module) and it tells me that vfree
expects const void *
while I am passing it volatile type *
.
I understand how it can be dangerous to pass a volatile type *
as a type *
because in that function, the values of name[i]
could be cached (as a result of optimization) which is not desirable.
I don’t understand why though, vfree
expects me to send it a pointer necessarily to non-volatile data. Is there something I am missing there? Or is it just the guys who wrote vfree
not thinking about this situation?
I assume me simply casting my pointer to void *
would not cause any harm, is that right?