What is volatile?

160 views
Skip to first unread message

Anil Akkur

unread,
Mar 17, 2004, 1:02:58 AM3/17/04
to
Hi All,
Please explain me exact meaning of "volatile" qualifier? Where exactly we use it?
Please explain me with a code snippet.
Thanks in advance.

Mike Wahler

unread,
Mar 17, 2004, 1:59:10 AM3/17/04
to

"Anil Akkur" <anil...@yahoo.co.in> wrote in message
news:d46b1147.04031...@posting.google.com...

The 'volatile' qualifier tells the compiler that an
object can or will obtain its value from somewhere
outside direct control of the program (such as
an external device). This prohibits the compiler
from performing any optimizations which might disable
this value modification (the compiler might determine
that the program itself makes no changes and treat
its value as a constant). Also note that the same
object can also be qualified as 'const' (preventing
the program itself from modifying it), leading to
the seemingly contradictory but valid construct:

const volatile int i;

-Mike


Alex Vinokur

unread,
Mar 17, 2004, 2:52:56 AM3/17/04
to

"Anil Akkur" <anil...@yahoo.co.in> wrote in message news:d46b1147.04031...@posting.google.com...

--------------------------------------------------
Compiler : GNU gcc version 3.3.1 (cygming special)
--------------------------------------------------


====== C code : BEGIN ======
------ File foo1.cpp -------
int main(int argc, char** argv)
{
int i;
i = argv[1][0];
i = argv[1][1];
return i;
}
----------------------------


------ File foo2.cpp -------
int main(int argc, char** argv)
{
volatile int i;
i = argv[1][0];
i = argv[1][1];
return i;
}
----------------------------

====== C code : END ========


====== Compilation (No optimization): BEGIN ========

$ gcc -save-temps foo1.c

$ gcc -save-temps foo2.c

$ ls foo*.s
foo1.s foo2.s // Files containing assembler code

$ diff foo1.s foo2.s
1c1
< .file "foo1.c"
---
> .file "foo2.c"

====== Compilation (No optimization): END ==========


====== Compilation (Optimization O1): BEGIN ========

$ gcc -O1 -save-temps foo1.c

$ gcc -O1 -save-temps foo2.c

$ ls foo*.s
foo1.s foo2.s // Files containing assembler code

$ diff foo1.s foo2.s
1c1
< .file "foo1.c"
---
> .file "foo2.c"
15,16c15,20
< movl 4(%eax), %eax
< movsbl 1(%eax),%eax
---
> movl 4(%eax), %edx
> movsbl (%edx),%eax
> movl %eax, -4(%ebp)
> movsbl 1(%edx),%eax
> movl %eax, -4(%ebp)
> movl -4(%ebp), %eax


====== Compilation (Optimization O1): END ==========


Conclusion.

No optimization : Assembler code for foo1.c and foo2.c is identical.
Optimization O1 : Assembler code for foo1.c and foo2.c is different.


--
Alex Vinokur
mailto:ale...@connect.to
http://mathforum.org/library/view/10978.html


Pierre Maurette

unread,
Mar 17, 2004, 5:21:41 AM3/17/04
to
"Mike Wahler" <mkwa...@mkwahler.net> a écrit ...

>
> "Anil Akkur" <anil...@yahoo.co.in> wrote in message
> news:d46b1147.04031...@posting.google.com...
> > Hi All,
> > Please explain me exact meaning of "volatile" qualifier? Where exactly
we
> use it?
> > Please explain me with a code snippet.
> > Thanks in advance.
>
> The 'volatile' qualifier tells the compiler that an
> object can or will obtain its value from somewhere
> outside direct control of the program (such as
> an external device).
or other thread/processus. More:
I prefer to consider a volatile object being able to:
- be modified "externally" at any time.
- modify the environment (side effects) when accessed at any time.
- be read by the "outside" of the program at any time.
Microsoft 7.1 compiler (self named optimizing compiler) understands volatile
this way, and so is very predictable (for me).
Every operation on a volatile memory variable must be machine coded. If the
operation takes place in a register, the variable must be read from memory,
operated, and then saved to memory, even if the next C instruction leads to
read it from memory to the same register.

> This prohibits the compiler
> from performing any optimizations which might disable
> this value modification (the compiler might determine
> that the program itself makes no changes and treat
> its value as a constant). Also note that the same
> object can also be qualified as 'const' (preventing
> the program itself from modifying it), leading to
> the seemingly contradictory but valid construct:
>
> const volatile int i;

Sure. const volatile is very meaningfull, an input port for reading
configuration switches for exemple.

volatile is usefull to force code generation, for example for studying or
debugging purposes. You also can think about memory test/stress routines to
force write/read cycles.
And obviously for good hardware and multitask oriented resons!

Code example:
(Microsoft C/C++ 7.1, release settings with /FAcs for assembly listing)
--------------------------------------------------------------
--------------------------------------------------------------
int main(void)
{
/*volatile*/ int a = 10;
/*volatile*/ int b = 2;
a += b--;
a += b--;
b++;
b--;
a = 10 / b;
return 0;
}
--------------------------------------------------------------
--------------------------------------------------------------
; 5 : /*volatile*/ int a = 10;
; 6 : /*volatile*/ int b = 2;
; 7 : a += b--;
; 8 : a += b--;
; 9 : b++;
; 10 : b--;
; 11 : a = 10 / b;
; 12 : return 0;

00000 33 c0 xor eax, eax

; 13 : }

00002 c3 ret 0
--------------------------------------------------------------
--------------------------------------------------------------
Like this, the compiler generate nothing, and there is no "divide by 0"
exception.

Other tests (commenting and uncommenting) showing that this compiler is
subtly attentive to volatile modifier and optimization:
--------------------------------------------------------------
--------------------------------------------------------------
; 5 : /*volatile*/ int a = 10;
; 6 : volatile int b = 2;

00001 c7 04 24 02 00
00 00 mov DWORD PTR _b$[esp+4], 2

; 7 : a += b--;

00008 8b 04 24 mov eax, DWORD PTR _b$[esp+4]
0000b 8b 0c 24 mov ecx, DWORD PTR _b$[esp+4]
0000e 49 dec ecx
0000f 89 0c 24 mov DWORD PTR _b$[esp+4], ecx

; 8 : a += b--;

00012 8b 14 24 mov edx, DWORD PTR _b$[esp+4]
00015 8b 04 24 mov eax, DWORD PTR _b$[esp+4]
00018 48 dec eax
00019 89 04 24 mov DWORD PTR _b$[esp+4], eax

; 9 : b++;

0001c 8b 0c 24 mov ecx, DWORD PTR _b$[esp+4]
0001f 41 inc ecx
00020 89 0c 24 mov DWORD PTR _b$[esp+4], ecx

; 10 : b--;

00023 8b 14 24 mov edx, DWORD PTR _b$[esp+4]
00026 4a dec edx
00027 89 14 24 mov DWORD PTR _b$[esp+4], edx

; 11 : a = 10 / b;

0002a 8b 04 24 mov eax, DWORD PTR _b$[esp+4]

; 12 : return 0;

0002d 33 c0 xor eax, eax

; 13 : }

0002f 59 pop ecx
00030 c3 ret 0
--------------------------------------------------------------
--------------------------------------------------------------
; 5 : volatile int a = 10;

00001 b8 0a 00 00 00 mov eax, 10 ; 0000000aH
00006 89 04 24 mov DWORD PTR _a$[esp+4], eax

; 6 : /*volatile*/ int b = 2;
; 7 : a += b--;

00009 8b 0c 24 mov ecx, DWORD PTR _a$[esp+4]
0000c 83 c1 02 add ecx, 2
0000f 89 0c 24 mov DWORD PTR _a$[esp+4], ecx

; 8 : a += b--;

00012 8b 14 24 mov edx, DWORD PTR _a$[esp+4]
00015 42 inc edx
00016 89 14 24 mov DWORD PTR _a$[esp+4], edx

; 9 : b++;
; 10 : b--;
; 11 : a = 10 / b;

00019 99 cdq
0001a 33 c9 xor ecx, ecx
0001c f7 f9 idiv ecx
0001e 89 04 24 mov DWORD PTR _a$[esp+4], eax

; 12 : return 0;

00021 33 c0 xor eax, eax

; 13 : }

00023 59 pop ecx
00024 c3 ret 0
--------------------------------------------------------------
--------------------------------------------------------------
00000 83 ec 08 sub esp, 8

; 5 : volatile int a = 10;

00003 b8 0a 00 00 00 mov eax, 10 ; 0000000aH
00008 89 44 24 04 mov DWORD PTR _a$[esp+8], eax

; 6 : volatile int b = 2;

0000c c7 04 24 02 00
00 00 mov DWORD PTR _b$[esp+8], 2

; 7 : a += b--;

00013 8b 0c 24 mov ecx, DWORD PTR _b$[esp+8]
00016 03 4c 24 04 add ecx, DWORD PTR _a$[esp+8]
0001a 89 4c 24 04 mov DWORD PTR _a$[esp+8], ecx
0001e 8b 14 24 mov edx, DWORD PTR _b$[esp+8]
00021 4a dec edx
00022 89 14 24 mov DWORD PTR _b$[esp+8], edx

; 8 : a += b--;

00025 8b 0c 24 mov ecx, DWORD PTR _b$[esp+8]
00028 03 4c 24 04 add ecx, DWORD PTR _a$[esp+8]
0002c 89 4c 24 04 mov DWORD PTR _a$[esp+8], ecx
00030 8b 14 24 mov edx, DWORD PTR _b$[esp+8]
00033 4a dec edx
00034 89 14 24 mov DWORD PTR _b$[esp+8], edx

; 9 : b++;

00037 8b 0c 24 mov ecx, DWORD PTR _b$[esp+8]
0003a 41 inc ecx
0003b 89 0c 24 mov DWORD PTR _b$[esp+8], ecx

; 10 : b--;

0003e 8b 14 24 mov edx, DWORD PTR _b$[esp+8]
00041 4a dec edx
00042 89 14 24 mov DWORD PTR _b$[esp+8], edx

; 11 : a = 10 / b;

00045 99 cdq
00046 f7 3c 24 idiv DWORD PTR _b$[esp+8]
00049 89 44 24 04 mov DWORD PTR _a$[esp+8], eax

; 12 : return 0;

0004d 33 c0 xor eax, eax

; 13 : }

0004f 83 c4 08 add esp, 8
00052 c3 ret 0
--------------------------------------------------------------
--------------------------------------------------------------

Pierre

Darrell Grainger

unread,
Mar 17, 2004, 9:37:16 AM3/17/04
to
On Wed, 16 Mar 2004, Anil Akkur wrote:

> Hi All,
> Please explain me exact meaning of "volatile" qualifier? Where exactly
> we use it?

The volatile modifier tells the compiler that something outside of the
program might be using this variable. Because the compiler cannot know
what that outside influence is doing it will avoid optimizing the
variable. Many times an optimizer will realize the variable is there for
readability. The machine does not need it so it gets optimized away in the
machine code.

> Please explain me with a code snippet.

Hard to do without knowing the environment you are working in. I use the
volatile modifier when working on embedded systems. There is usually no
operating system. Let's say that memory location 0x400 holds the processor
clock. This clock is updated by the hardware automatically. So I might
have something like:

#include <stdio.h>

int main(void)
{
volatile const int *clock = (const int *)0x400;
int start, end, diff;

start = *clock;
/* do some stuff */
end = *clock;

diff = end - start;
printf("stuff took %d ticks.\n", diff);
return 0;
}

If I did not have the modifier volatile on clock then the compiler would
assume that clock never changes because there is nothing in the program
that changes it. It could then assume that start will equal end and that
diff will equal 0. It could then optimize away clock, end, start and diff
then just print 0.

--
Send e-mail to: darrell at cs dot toronto dot edu
Don't send e-mail to vice.pr...@whitehouse.gov

Mark A. Odell

unread,
Mar 17, 2004, 9:55:21 AM3/17/04
to
"Mike Wahler" <mkwa...@mkwahler.net> wrote in
news:2HS5c.25474$%06.2...@newsread2.news.pas.earthlink.net:

> Also note that the same
> object can also be qualified as 'const' (preventing
> the program itself from modifying it), leading to
> the seemingly contradictory but valid construct:
>
> const volatile int i;

Seems normal to me. This is how I'd declare a memory-mapped, read-only
hardware status register.

--
- Mark ->
--

Dan Pop

unread,
Mar 17, 2004, 11:27:41 AM3/17/04
to

You also need "extern" for that.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Dan...@ifh.de

Mike Wahler

unread,
Mar 17, 2004, 1:11:26 PM3/17/04
to

"Mark A. Odell" <nos...@embeddedfw.com> wrote in message
news:Xns94AF64F042736C...@130.133.1.4...

I meant that it might 'seem contradictory' to anyone considering
the English meanings of 'constant' and 'volatile'. But of course
C isn't English ... :-)

-Mike


guna

unread,
Mar 19, 2004, 12:44:26 PM3/19/04
to
hello friend

in c all the value of variable kept in memory for some
amount of time. if u are using volatile the value of variable
immediately destroy from memory after use, it is not kept in main
memory.

guna.

"Mike Wahler" <mkwa...@mkwahler.net> wrote in message news:<ix06c.44765$aT1...@newsread1.news.pas.earthlink.net>...

Irrwahn Grausewitz

unread,
Mar 19, 2004, 1:06:49 PM3/19/04
to
mali...@yahoo.com (guna) wrote:
>hello friend
>
> in c all the value of variable kept in memory for some
>amount of time. if u are using volatile the value of variable
>immediately destroy from memory after use, it is not kept in main
>memory.

Utter nonsense.

>
>"Mike Wahler" <mkwa...@mkwahler.net> wrote:
<snip>

If Mike Wahler were your friend, you'd already know that
top-posting is rude and Mike certainly knows what he's
talking about... :-)

Regards
--
Irrwahn Grausewitz (irrw...@freenet.de)
welcome to clc : http://www.ungerhu.com/jxh/clc.welcome.txt
clc faq-list : http://www.faqs.org/faqs/C-faq/faq/
clc OT guide : http://benpfaff.org/writings/clc/off-topic.html

Xiangliang Meng

unread,
Mar 22, 2004, 2:00:40 AM3/22/04
to
Please consult this link. http://cermics.enpc.fr/~ts/C/SYNTAX/volatile.html


"guna" <mali...@yahoo.com> wrote in message
news:874b9b6e.04031...@posting.google.com...

Peter Nilsson

unread,
Mar 22, 2004, 4:51:00 AM3/22/04
to
"Xiangliang Meng" <xiangli...@hotmail.com> wrote in message
news:c3m2r4$96f$1...@zcars0v6.ca.nortel.com...

Please don't top-post.

Or better yet, don't. The site's philosophy seems to be: Blind Leading the
Blind.

--
Peter


Reply all
Reply to author
Forward
0 new messages