Suppose I have the following function:
void atomic_add(volatile unsigned * loc, unsigned incr );
And I have want to pass a variable to the first argument of this function.
unsigned a;
atomic_add(&a, 1);
volatile unsigned b;
atomic_add(&b, 1);
From volatile type qualifier, it should can be compiled.
Q1: Do both of them get the same results?
Q2: Does the pointer returned from mmap()
can also be used as the first argument of this function?
Thanks!
Different between volatile pointer pointing to volatile variable and non-volatile variable
First of all loc
is not a volatile pointer
. It is a pointer referencing a volatile object
You can pass volatile
and not volatile
to this function, bout localy in the function your ocject will be treated as volatile
. It means that it will be read before every use, and saved to its storage after every change. It will not be "buffered" in he registers. Even if you *loc + *loc
the referenced object will be read twice.
To have a volatile pointer you need to declare it different way:
unsigned * volatile loc; // volatile pointer to not volatile unsigned int object
volatile unsigned * volatile loc; // volatile pointer to volatile unsigned int object
volatile unsigned * loc; // non volatile pointer to volatile unsigned int object
Q1 - yes
Q2 - yes