Q: volatile struct elements or entire struct?

M

Mark A. Odell

If I have a structure that may point to a volatile "region (e.g. device)"
or a context in memory what would be the best way to use the volatile
keyword?

E.g.

a) volatile on struct objects

struct Foo
{
unsigned char command;
unsigned char status;
unsigned long baseAddr;
unsigned long addrRange;
}

volatile struct Foo *pDevFoo = (some dev. address);
struct Foo hostFoo;


- or -


b) volatile on struct members

struct Foo
{
volatile unsigned char command;
volatile unsigned char status;
volatile unsigned long baseAddr;
volatile unsigned long addrRange;
}

struct Foo *pDevFoo = (some dev. address);
struct Foo hostFoo; /* live with extra accesses */


Does my use of volatile in a) even do what I intend? That is, make
accesses to the device volatile?

Thanks.
 
C

Chris Torek

[which of these should one use:]
a) volatile on struct objects

struct Foo {
unsigned char command;
unsigned char status;
unsigned long baseAddr;
unsigned long addrRange;
}

volatile struct Foo *pDevFoo = (some dev. address);
struct Foo hostFoo;

This is the version I prefer, for several reasons, the most valid :)
being the one shown right here in your example: you can make a
non-volatile version of it.
b) volatile on struct members

struct Foo
{
volatile unsigned char command;
volatile unsigned char status;
volatile unsigned long baseAddr;
volatile unsigned long addrRange;
}

struct Foo *pDevFoo = (some dev. address);
struct Foo hostFoo; /* live with extra accesses */

Does my use of volatile in a) even do what I intend? That is, make
accesses to the device volatile?

Yes. Both methods work (provided, of course, that your implementation's
definition of "volatile" does what you need in the first place --
if not, neither method works anyway!).
 
M

Mark A. Odell

[which of these should one use:]
a) volatile on struct objects

struct Foo {
unsigned char command;
unsigned char status;
unsigned long baseAddr;
unsigned long addrRange;
}

volatile struct Foo *pDevFoo = (some dev. address);
struct Foo hostFoo;

This is the version I prefer, for several reasons, the most valid :)
being the one shown right here in your example: you can make a
non-volatile version of it.

Excellent.
Yes. Both methods work (provided, of course, that your implementation's
definition of "volatile" does what you need in the first place --
if not, neither method works anyway!).

Oh it does, this has been heavily verified. Cross-compilers tend to this
as one my hope for.

Thanks Chirs.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top