473,748 Members | 2,294 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

What is the proper use of volatile?


I have a struct that maps onto a set of memory mapped registers. I
access this via a pointer.

Is it better to declare it as pointer to a volatile struct, or to
declare the individual members as volatile?

That is to say, which is likely to be better/less error prone:

struct registers {
unsigned long reg1;
unsigned long reg2;
};
registers volatile *register_set;

or

struct registers {
unsigned long volatile reg1;
unsigned long volatile reg2;
};
registers *register_set;

Feb 27 '07 #1
6 2608
red floyd wrote:
I have a struct that maps onto a set of memory mapped registers. I
access this via a pointer.

Is it better to declare it as pointer to a volatile struct, or to
declare the individual members as volatile?

That is to say, which is likely to be better/less error prone:

struct registers {
unsigned long reg1;
unsigned long reg2;
};
registers volatile *register_set;

or

struct registers {
unsigned long volatile reg1;
unsigned long volatile reg2;
};
registers *register_set;
I think that if your registers can individually change, you need to
individually name them volatile. Reflects the true nature of them
better, methinks.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Feb 27 '07 #2
Victor Bazarov wrote:
red floyd wrote:
>I have a struct that maps onto a set of memory mapped registers. I
access this via a pointer.

struct registers {
unsigned long volatile reg1;
unsigned long volatile reg2;
};
registers *register_set;

I think that if your registers can individually change, you need to
individually name them volatile. Reflects the true nature of them
better, methinks.
Yeah, I had pretty much decided that, too. It's always nice to get some
validation/confirmation, though. Also lets me declare them "const
volatile" where needed.

Thanks, Victor.

red floyd
Feb 27 '07 #3
On Feb 27, 3:05 pm, red floyd <no.s...@here.d udewrote:
I have a struct that maps onto a set of memory mapped registers. I
access this via a pointer.

Is it better to declare it as pointer to a volatile struct, or to
declare the individual members as volatile?
Definitely the individual members. The reason is that this helps to
contain volatile from spreading throughout your program.
That is to say, which is likely to be better/less error prone:

struct registers {
unsigned long reg1;
unsigned long reg2;};

registers volatile *register_set;
See, the trouble is that now you cannot pass this into any function
that just takes a plain ``volatile *'' parameter. And so now you have
to start introducing volatile into function parameters and other local
variables all over the place.

If you ever have this type of structure and you need to put it into a
linked list, e.g.

struct dma_ring_buffer {
unsigned long reg1;
volatile dma_ring_buffer *next;
};

Now the volatile has invaded the interior of the structure: the member
``next'' is a pointer to a qualified structure in order that a
volatile-qualified pointer can be assigned to it.
or

struct registers {
unsigned long volatile reg1;
unsigned long volatile reg2;};

registers *register_set;
The end effect on the access semantics is the same. If P is a pointer
to a volatile-qualified structure then P->M, where M is a member of
that structure, is also volatile qualified. It makes no difference
which way you do it; either way ptr->reg1 accesses a volatile-
qualified unsigned long.

So pick the way that creates the fewest qualifier-related headaches in
your code.
Feb 28 '07 #4
red floyd wrote:
>
I have a struct that maps onto a set of memory mapped registers. I
access this via a pointer.

Is it better to declare it as pointer to a volatile struct, or to
declare the individual members as volatile?

That is to say, which is likely to be better/less error prone:

struct registers {
unsigned long reg1;
unsigned long reg2;
};
registers volatile *register_set;

or

struct registers {
unsigned long volatile reg1;
unsigned long volatile reg2;
};
registers *register_set;
Although the above considerations of Victor and Kaz can be valid in some
contexts, here is the one that may suggest the opposite solution:

Sometimes (often?) you would only want one instance of a structure to be
volatile (or, say, one per CPU...) -- for example where it is mapped
onto the particular hardware-specific memory range. If, additionally,
you need to store other instances of your set of registers, and
especially manipulate with these stored instances in a computationally
heavy way, you may want to avoid the runtime overhead of volatile in
those manipulations; in such a situation, you would have to define 2
structures instead of one if you selected the "volatile individual
members" approach whereas the "whole volatile structure" approach would
not require that.

-Pavel
Feb 28 '07 #5
On Feb 28, 8:53 am, Pavel <nos...@nospam. comwrote:
red floyd wrote:
I have a struct that maps onto a set of memory mapped registers. I
access this via a pointer.
Is it better to declare it as pointer to a volatile struct, or to
declare the individual members as volatile?
That is to say, which is likely to be better/less error prone:
struct registers {
unsigned long reg1;
unsigned long reg2;
};
registers volatile *register_set;
or
struct registers {
unsigned long volatile reg1;
unsigned long volatile reg2;
};
registers *register_set;

Although the above considerations of Victor and Kaz can be valid in some
contexts, here is the one that may suggest the opposite solution:

Sometimes (often?) you would only want one instance of a structure to be
volatile (or, say, one per CPU...) -- for example where it is mapped
onto the particular hardware-specific memory range. If, additionally,
you need to store other instances of your set of registers, and
especially manipulate with these stored instances in a computationally
heavy way, you may want to avoid the runtime overhead of volatile in
those manipulations; in such a situation, you would have to define 2
structures instead of one if you selected the "volatile individual
members" approach whereas the "whole volatile structure" approach would
not require that.

-Pavel- Hide quoted text -

- Show quoted text -
Just curious. What prevents from using const_cast to remove the
volatile-ness for manipulating the structure and the saving it again
(not really sure if cast back to voltile would be necessary again) .
Since the strcuture is not volatile qualified the affects on the
hardware if any due to the change in values are intended to be
discarded; the intermediate values of compuatation would be cached.

I agree with Kaz, I've seen code where the strcutures were volatile
instead of the individual members and the whole code was literred with
const_cast to remove volatile-ness. I'd rather, as Kaz says, keep the
individual members are volatile, since this is the true representation
of the behaviour and wihtout a const_cast literred code.
--
Taran

Feb 28 '07 #6
Kaz Kylheku wrote:
On Feb 27, 3:05 pm, red floyd <no.s...@here.d udewrote:
>I have a struct that maps onto a set of memory mapped registers. I
access this via a pointer.

Is it better to declare it as pointer to a volatile struct, or to
declare the individual members as volatile?

Definitely the individual members. The reason is that this helps to
contain volatile from spreading throughout your program.
>That is to say, which is likely to be better/less error prone:

struct registers {
unsigned long reg1;
unsigned long reg2;};

registers volatile *register_set;

See, the trouble is that now you cannot pass this into any function
that just takes a plain ``volatile *'' parameter. And so now you have
to start introducing volatile into function parameters and other local
variables all over the place.

If you ever have this type of structure and you need to put it into a
linked list, e.g.

struct dma_ring_buffer {
unsigned long reg1;
volatile dma_ring_buffer *next;
};

Now the volatile has invaded the interior of the structure: the member
``next'' is a pointer to a qualified structure in order that a
volatile-qualified pointer can be assigned to it.
This is a consideration that I hadn't thought of, and it's a very good
argument in favor of making the individual members volatile rather than
the struct itself.

This struct is actually a private member of an enclosing class, so that
nobody else sees it. But as I mentioned in my reply to Victor, I'd
pretty much decided that this is the way to go anyways.

I want to thank everyone for their feedback, as it provided some good
food for thought for future designs.
Feb 28 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

125
14807
by: Sarah Tanembaum | last post by:
Beside its an opensource and supported by community, what's the fundamental differences between PostgreSQL and those high-price commercial database (and some are bloated such as Oracle) from software giant such as Microsoft SQL Server, Oracle, and Sybase? Is PostgreSQL reliable enough to be used for high-end commercial application? Thanks
4
1915
by: Andrew | last post by:
Section 17.4.3 of the ECMA-334 C# Language Specification says 1 When a field-declaration includes a volatile modifier, the fields introduced by that declaration are volatile fields. 2 For non-volatile fields, optimization techniques that reorder instructions can lead to unexpected and unpredictable results in multi-threaded programs that access fields without synchronization such as that provided by the lock-statement (15.12). 3 These...
6
1489
by: nin234ATIyahoo.com | last post by:
Hi all I have a logger class and facing the following issue enum eErrLvl { eLOG_DEBUG_5 = 1, eLOG_DEBUG_4, eLOG_DEBUG_3, eLOG_DEBUG_2, eLOG_DEBUG_1,
8
1651
by: dwaach | last post by:
what does T*VQ& mean where T is object type, VQ is volatile or empty, Is this correct, struct X {}; volatile X ox;
4
5819
by: dwaach | last post by:
Hi, I have something like. struct X {}; X ox; X* pox=&ox; X*& volatile r =pox;
5
7912
by: ben | last post by:
Hello All, I am trying to make sense of a bit of syntax, is there a guru out there that can clear this up for me. I have a buffer declared as static volatile u8 buffer; and I have a pointer to that buffer declared as
6
2334
by: bwaichu | last post by:
Can someone help me out here? This is one of those areas of C that I am fuzzy on. I'm not quite sure when I should use volatile. If you could provide an example, that would help out a lot. I know it has to do with the expectation that the variable can be changed outside of the control of the program. I know it has to do with telling the compiler to not optimize that piece of code, so that the variable can be changed from outside of...
33
5698
by: James H. Newman | last post by:
I have a portion of code along the following lines: volatile unsigned char x ; unsigned int f(unsigned char *y) ; When I do unsigned int z = f(&x) ;
4
2131
by: Gestorm | last post by:
Hi all, I found a macro "USE_VAR" in the code of bash-3.2 as follows: /*file: bash-3.2/shell.c*/ 344 USE_VAR(argc); 345 USE_VAR(argv); 346 USE_VAR(env); 347 USE_VAR(code); 348 USE_VAR(old_errexit_flag); 349 #if defined (RESTRICTED_SHELL) 350 USE_VAR(saverst);
0
8822
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9528
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9359
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9236
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8235
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4592
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3298
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2774
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2206
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.