Q) Volatile type or qualifier?

325 просмотров
Перейти к первому непрочитанному сообщению

juli...@usa.net

не прочитано,
10 мар. 2001 г., 13:49:1010.03.2001
Hello everyone


What effect does volatile have on the compiler or code? Does
it tell the compiler not to optimize that value declared as volatile?

What would one use volatile for ?

My take on volatile is that it is not implementation independent.
Meaning
the assembly language produced in dependent on the c compiler and
OS used on the same hardware, ie Win 9x and Linux implement volatile
differently ?

Volatile is part of ANSI C and suppresses optimization of the object
it refers to or points to?

TIA


js

Mark Duell

не прочитано,
10 мар. 2001 г., 14:29:5610.03.2001
<juli...@usa.net> wrote in message news:3aaa76e...@news.igs.net...

Im not sure about the optimization questions, but the type qualifier
volatile indicates that a variable's storage cell might be referenced by
something besides statements in the program that defines the variable. In
other words, other programs can read/write that variable.

Mark Duell


Kaz Kylheku

не прочитано,
10 мар. 2001 г., 16:16:4610.03.2001
On Sat, 10 Mar 2001 18:49:10 GMT, juli...@usa.net <juli...@usa.net> wrote:
>Hello everyone
>
>
>What effect does volatile have on the compiler or code? Does
>it tell the compiler not to optimize that value declared as volatile?
>
>What would one use volatile for ?

In standard C programming, you may have to declare some automatic
variables volatile if you are using setjmp and longjmp. In the function
that records a context via setjmp, if there are auto variables that are
modified after the setjmp, but prior to the return via longjmp, those
variables need to be volatile if their values are accessed after
returning via longjmp. Otherwise the access invokes undefined behavior.

Secondly, a signal handler may store a value to a static object of type
volatile sig_atomic_t; the normal execution of the program can access
such an object to (for example) determine that the signal handler
function executed.

>Volatile is part of ANSI C and suppresses optimization of the object
>it refers to or points to?

No, because ANSI C doesn't specify optimization. Other than the two
uses I desribed, volatile has no further meaning, because you can't
write a standard C program that can tell the difference. A program that
behaves differently when it's optimized most likely has some kind of
bug whereby it relies on undefined or, at best, unspecified behavior.
Of course, a conforming compiler must produce a diagnostic if a
translation unit violates a constraint rule relating to volatile,
such as assigning a ``volatile T *'' pointer value to an unqualified
pointer.

Compilers that support operating system programming tend to use
volatile to provide special semantics, making volatile useful for
things like accesses to memory-mapped hardware, or global variables
that are changed by interrupt service routines (e.g. an incrementing
clock tick counter).

Ответить всем
Написать сообщение автору
Переслать
0 новых сообщений