c - what is extern volatile pointer - stack overflow
TRANSCRIPT
-
8/16/2019 c - What is Extern Volatile Pointer - Stack Overflow
1/3
10/05/2016 c - What is extern volatile pointer - Stack Overflow
http://stackoverflow.com/questions/14332503/what-is-extern-volatile-pointer
sign up log in tour help
_ Stack Overflow is a community of 4.7
million programmers, just like you,
helping each other.
Join them; it only takes a minute:
Sign up
Join the Stack Overflow community to:
Ask programming
questions
Answer and help
your peers
Get recognized for your
expertise
What is extern volatile pointer
What is extern volatile pointer.
extern volatile uint32 *ptr;
Here, what will be the behavior of *ptr? What does this actually mean?
And, when it should be used?
I have tried to google about it, but didn't get any satisfactory answer, there is not much info present about this
combination.
c volatile
edited Jan 25 '13 at 8:26 asked Jan 15 '13 at 6:52
Arti
108 2 8
–You didn't look really hard see stackoverflow.com/questions/9935190/… Mikhail Jan 15 '13 at 6:54
–
Extern and volatile are not related. You have a volatile pointer, and it happens to be defined somewhere else.
The extern qualifier doesn't change the semantics of the volatile pointer. Mat Jan 15 '13 at 6:56
–
I have seen extern volatile *ptr usage in the project I am working in, but due to high data security reasons, I
can not provide the code, for refernce. Also, since product is very vast, so I am unable to understand its
usage. Arti Jan 15 '13 at 6:58
–
tells the compiler to read from its memory location whenever its value is read in the
program/scope. Usually the compiler reads small variables (int, pointer) into a CPU register and use that
register for the next sequential references to that value (for the sake of efficiency). Using makes the
compiler stop that behavior.
volatile obj obj
volatile
ringø Jan 15 '13 at 7:06
–
Can anyone answer, considering both "exter volatile" please? I am not looking for individual definitions. like
what will be the system behaviour if I omit volatile here, what is the behaviour with 'extern volatile'. any
implementaion examples will be helpful. Arti Jan 15 '13 at 7:42
4 Answers
Both and keywords can be considered independently. The role of each of these
keywords does not interact with the other one, and thus an explanation of each of them can be
detailed independently, as below.
extern volatile
tells the compiler that the actual definition of is in another module (another ).
Basically, there is no much change to how the compiler handles - having just tells the
compiler that it does not have to reserve some space in memory for as it is done elsewhere in
another , and its actual memory location will be given by the later.
extern ptr .c
ptr extern
ptr
.c linker
extern uint32 *ptr;
If you omit the compiler will not complain. However, later, the , when it tries to link all
object modules in order to build the final executable program, will throw an error saying " is
defined twice" (since it is already defined in another ).
extern linker
ptr
.c
uint32 *ptr;
tells the compiler that the memory location where resides may be altered / modified
by some external event, and it (the compiler) should not rely on some efficiency optimizations like
considering that the value of will not change during the scope of a few sequential lines of C.
Such an event may be an asynchronous interruption, that happens when the CPU executes the
volatile ptr
ptr
http://stackoverflow.com/users/1354398/artihttp://stackexchange.com/https://stackoverflow.com/users/signup?ssrc=head&returnurl=http%3a%2f%2fstackoverflow.com%2fquestions%2f14332503%2fwhat-is-extern-volatile-pointerhttps://stackoverflow.com/users/login?ssrc=head&returnurl=http%3a%2f%2fstackoverflow.com%2fquestions%2f14332503%2fwhat-is-extern-volatile-pointerhttp://stackoverflow.com/tourhttp://stackoverflow.com/users/1354398/artihttp://stackoverflow.com/users/338904/ring%c3%b8http://stackoverflow.com/users/1354398/artihttp://stackoverflow.com/users/635608/mathttp://stackoverflow.com/users/314290/mikhailhttp://stackoverflow.com/questions/9935190/why-is-a-point-to-volatile-pointer-like-volatile-int-p-usefulhttp://stackoverflow.com/users/1354398/artihttp://stackoverflow.com/posts/14332503/revisionshttp://stackoverflow.com/questions/tagged/volatilehttp://stackoverflow.com/questions/tagged/chttp://stackoverflow.com/questions/14332503/what-is-extern-volatile-pointerhttp://stackoverflow.com/users/signup?ssrc=hero&returnurl=http%3a%2f%2fstackoverflow.com%2fquestions%2f14332503%2fwhat-is-extern-volatile-pointerhttp://stackoverflow.com/tourhttps://stackoverflow.com/users/login?ssrc=head&returnurl=http%3a%2f%2fstackoverflow.com%2fquestions%2f14332503%2fwhat-is-extern-volatile-pointerhttps://stackoverflow.com/users/signup?ssrc=head&returnurl=http%3a%2f%2fstackoverflow.com%2fquestions%2f14332503%2fwhat-is-extern-volatile-pointerhttp://stackexchange.com/
-
8/16/2019 c - What is Extern Volatile Pointer - Stack Overflow
2/3
10/05/2016 c - What is extern volatile pointer - Stack Overflow
http://stackoverflow.com/questions/14332503/what-is-extern-volatile-pointer
above said scope, and modifies the value. ptr
Typically (with virtual assembly code of optimized C in comments), REGx are CPU registers, and
we are not much interested in the variable ...y
int x = 10;
int func() {
int y; // REG4
printf("%d\n", x); // Set REG3 = memory(x) and display x
x += 2; // Add 2 to REG3
y = x * x; // REG4 = REG3 * REG3
printf("%d %d\n", x, y); // Do printf(..., REG3, REG4)
x += 5; // REG3 = REG3 + 5
// memory(x) = REG3 (save register to memory) return y; // return REG4
}
should display . For the sake of efficiency (memory accesses are more costly than
register accesses) say the compiler stores the value of in an internal CPU register (REG3) and
uses it in safely until the end where it stores the new value of (it's a global var) to
memory location. is 17 at the end.
10, 12, 144
x
func x x
x
But imagine the program is more complex than that and has a clock interruption every minute that
subtract 10 to . What happens if an interruption... x
void inter_call_by_timer_every_minute() {
x -= 10;
}
... occurs in say just after the line? has in REG3 (10), add 2 (12)
and finally add 5 (17) and stores the REG3 result to memory location (17). This is wrong as theinterruption effect (-10) has been hidden by the compiler optimization, since it stores the value
from REG3 to memory(x) at the end ignoring the done by the interruption. The correct
result was: is initially 10, interrupt subtract 10 to it (0) after the first in , then 2 is
added, then 5. Result 7.
func printf("%d\n", x); func x
x
subtract
x printf func
Adding volatile
volatile int x = 10;
will have the compiler avoid the optimizations in x func
int func() {
int y; // REG4
printf("%d\n", x); // display memory(x)
x += 2; // memory(x) += 2
y = x * x; // REG4 = memory(x) * memory(x)
printf("%d %d\n", x, y); // Do printf(..., memory(x), REG4)
x += 5; // memory(x) += 5
return y; // return REG4
}
and read the value of from memory all the time. Result, having an interruption from
after the first printf, is == 7.
x
inter_call_by_timer_every_minute x
edited Apr 4 at 16:02
rphv
2,650 1 10 31
answered Jan 15 '13 at 8:37
ringø
15.1k 4 31 65
I would explain the way I know through the keywords. means the variable is defined for
use somewhere outside the definition scope. For example it could be defined in header file and
get used in .c file.
extern
means that modification of that variable should be consistent to the external world.
This means that all update should be committed to the main memory so that it can be seen by
other threads which share the same execution space.
volatile
answered Jan 15 '13 at 6:57
Hassan TM
1,560 6 15
Extern means it is defined elsewhere - probably in the header file. Volatile is info for the compiler
hat it shouldn't try to optimize this.
answered Jan 15 '13 at 6:56
http://stackoverflow.com/users/1952879/hassan-tmhttp://stackoverflow.com/users/338904/ring%c3%b8http://stackoverflow.com/users/1612562/rphvhttp://stackoverflow.com/posts/14333841/revisions
-
8/16/2019 c - What is Extern Volatile Pointer - Stack Overflow
3/3
10/05/2016 c - What is extern volatile pointer - Stack Overflow
http://stackoverflow.com/questions/14332503/what-is-extern-volatile-pointer
KamikazeCZ
515 5 19
2
–
Probably in a header file, it is uncommon to define variables in header files and most of the time also
bad practice. You will likely find extern in the header filer and the actual definition in a corresponding C file.
not
Lundin Jan 15 '13 at 7:54
keyword is used to declare a global variable which is defined somewhere else (that
means its defined in some other file).
extern
.c
For example consider in a project two files and are there. In that a global variable
is defined in , and that variable can be accessed in all the functions which are defined in that
file. If we want to access that same global variable in our second file , then that variable
should be declared as in
.c a.c b.c
a.c
b.c
extern b.c
file is given belowa.c
int flag = 0;
int main()
{
.......
func1();
printf("\nflag value is %d\n", flag).
.......
}
file is given belowb.c
extern int flag;void func1();
{
.....
flag = 10;
.....
}
keyword is used to inform the compiler to avoid doing any sort of optimization while
generating executable instructions.
volatile
int flag = 0;
int main()
{
while(flag == 0);
printf("\nflag value is %d\n", flag);
return 0;
}
Consider the above program, all compiler will optimize the as .
Because in the code there is no where value gets updated before that loop. So if
hat variable value gets updated by some other hardware then it will never get reflected in the
program execution. So if we declare that variable as like below, compiler will not
perform any optimization for that variable, and the behviour of that program will be as intended.
while(flag == 0); while(1);
flag while
volatile
volatile int flag = 0;
But if there is no way for the value of program's variable is getting updated by other hardware,
hen no need to declare that variable as . Because for valatile variables, CPU needs to
perform I/O operation for each instructions which is accessing that variable. This impact in
performance needs to be consider for a variable which will never get updated by other hardwares.
volatile
answered Jan 15 '13 at 10:16
raja ashok
3,456 9 32 57
http://stackoverflow.com/users/596370/raja-ashokhttp://stackoverflow.com/users/584518/lundinhttp://stackoverflow.com/users/1978950/kamikazecz