[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c

Re: 'volatile' semantics

Glenn

7/7/2011 7:22:00 AM

On 7 Jul., 06:21, Tim <t...@seemywebsite.please> wrote:
> Is there a difference between
>
> typedef struct SBob
> {
>   int a;
>   int b;
>   int c;
>
> } SBob;
>
> volatile extern Sbob gBob;
>
> and
>
> typedef struct SBob
> {
>   volatile int a;
>   volatile int b;
>   volatile int c;
>
> } SBob;
>
> extern Sbob gBob;
>
> ?
>
> Assuming that there's a peripheral out there named Bob, of course.
>
> --
> Tim Wescott
> Control system and signal processing consultingwww.wescottdesign.com

Hi Tim

Yes - there ought to a difference:

volatile "forces" the reading of the variable directly from its source
function/address every time the value is needed.

The source e.g. could be an internal register of a separate chip
connected via some bus or poit-to-point.

So you can not rely on a local variable value copy - it might have
been updated several times - or the reading itself have side-effect -
e.g. the side-effect might be a fresh sampling of som signal in the
other chip and some other-chip-local book-keeping.

-

Volatile variable:
http://en.wikipedia.org/wiki/Volatil...
Quote: "...
In C, and consequently C++, the volatile keyword was intended to[1]

* allow access to memory mapped devices
* allow uses of variables between setjmp and longjmp
* allow uses of sig_atomic_t variables in signal handlers.
...."

br,

Glenn