473,394 Members | 1,802 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Use of the Volatile keyword for a pointer to a volatile memory block

ben
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

static volatile u8 * volatile pBuffer;

pBuffer = &buffer; // easy right

What I am trying to achieve is to create a pointer to a buffer. I want
the pointer to be volatile and I want the buffer it is pointing to to
be volatile.

The reason I am doing this is to apply the misra guidlines so I am
already aware that becuase the pointer is volatile the code will be
forced to dereference each time it is used.

The declarations above work okay, compile and run, but I cannot
understand why the second volatile apears between the type and name "*
volatile pBuffer". If I place it before the type

static volatile u8 volatile * pBuffer; // this fails

it doesnt work. but keywords appear before the type dont they ?

Also, it may just be the crappy compiler i am using but if I declare
the pointer as a constant, pointing at a volatile buffer,

static const u8 * volatile pBuffer;

I seem to be able to assign the pointer a value at run time. This
makes no sense to me.

Can anyone add parentheis to my declarations so I can read my own code
and feel sure the volatile keyword is doing what I want it to do ?

Thanks all in advance.
Nov 14 '05 #1
5 7880

"ben" <mi********@hotmail.com> wrote in message
news:8d**************************@posting.google.c om...
<snip>
static volatile u8 buffer; <snip> static volatile u8 * volatile pBuffer;

pBuffer = &buffer; // easy right
<snip>
The declarations above work okay, compile and run, but I cannot
understand why the second volatile apears between the type and name "*
volatile pBuffer". If I place it before the type

static volatile u8 volatile * pBuffer; // this fails

it doesnt work. but keywords appear before the type dont they ?
Not always. This case is one exception. In this case the content of the
buffer is volatile. That is, the object your pointer is pointing *at* is
volatile, not the actual pointer to it.
Also, it may just be the crappy compiler i am using but if I declare
the pointer as a constant, pointing at a volatile buffer,

static const u8 * volatile pBuffer;

I seem to be able to assign the pointer a value at run time. This
makes no sense to me.


Actually it does. The *pointer* is constant, but the object *pointed at*
isn't. So the declaration makes perfect sense.

HTH dandelion.
Nov 14 '05 #2
ben wrote:
static volatile u8 buffer;
static volatile u8 * volatile pBuffer;
pBuffer = &buffer; // easy right [...] The declarations above work okay, compile and run, but I cannot
understand why the second volatile apears between the type and name "*
volatile pBuffer". If I place it before the type

static volatile u8 volatile * pBuffer; // this fails

it doesnt work. but keywords appear before the type dont they ?
No, that's just the exception. cv-qualifiers (vc meaning 'constant
volatile', as they are basically handled the same way) always appear after
the thing they apply to, with the exception that if they are on the
leftmost side of a declaration they apply to the thing on their right.

So:
volatile u8* ptr; // exception
u8 volatile* ptr; // canonical form

For variable declarations, the rule is to read the thing from the right to
the left, like "ptr is a pointer to a volatile u8". So if you want a
volatile pointer to an const double you would use
double const* volatile pd;

Don't ask me why people still use the exceptional way instead of wrapping
their minds around the canonical form. This was and still is the source of
heated discussion that usually generates more heat than light; I won't
dive too deep into this argument...
Also, it may just be the crappy compiler i am using but if I declare
the pointer as a constant, pointing at a volatile buffer,

static const u8 * volatile pBuffer;

I seem to be able to assign the pointer a value at run time. This
makes no sense to me.


There is not const pointer here, just a volatile pointer to a const u8.

Uli

Nov 14 '05 #3
In article <34*************@individual.net>,
Ulrich Eckhardt <do******@knuut.de> wrote:
... cv-qualifiers (cv meaning 'constant volatile', as they are
basically handled the same way) always appear after the thing
they apply to, with the exception that if they are on the leftmost
side of a declaration they apply to the thing on their right.

So:
volatile u8* ptr; // exception
u8 volatile* ptr; // canonical form

For variable declarations, the rule is to read the thing from the right to
the left, like "ptr is a pointer to a volatile u8". So if you want a
volatile pointer to an const double you would use
double const* volatile pd;

Don't ask me why people still use the exceptional way instead of wrapping
their minds around the canonical form. ...


Probably because "const" and "volatile" *look* like storage-class
specifiers, and "static int i = 3" is more common than "int static
i = 3".

(I have in the past argued that const should have been an sc-specifier,
and I still think this would be less confusing and more consistent;
but "volatile" actually does make sense as a type-qualifier, since
a pointer and its target may well both be "things secretly fiddled
by hardware behind the compiler's back".)
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Nov 14 '05 #4
On Mon, 10 Jan 2005 19:47:01 +0100, Ulrich Eckhardt wrote:
ben wrote:
static volatile u8 buffer;
static volatile u8 * volatile pBuffer;
pBuffer = &buffer; // easy right [...]
The declarations above work okay, compile and run, but I cannot
understand why the second volatile apears between the type and name "*
volatile pBuffer". If I place it before the type

static volatile u8 volatile * pBuffer; // this fails

it doesnt work. but keywords appear before the type dont they ?


No, that's just the exception. cv-qualifiers (vc meaning 'constant
volatile', as they are basically handled the same way) always appear after
the thing they apply to, with the exception that if they are on the
leftmost side of a declaration they apply to the thing on their right.


Type specifier/qualifiers/storage class specifiers and inline in C99 can
appear in any order, so that isn't entirely correct.
So:
volatile u8* ptr; // exception
u8 volatile* ptr; // canonical form
Only according to your specification of how the canonocal form should be
constructed. The more normal canonocal form is:

volatile u8 *ptr;
For variable declarations, the rule is to read the thing from the right
to the left, like "ptr is a pointer to a volatile u8".
Not true in general when arrays or functions are involved. The correct
analysis of a declarator is a syntactic one, similar to the analysis of an
expression. Right to left is misleading, although it works some of the
time.
So if you want a
volatile pointer to an const double you would use
double const* volatile pd;
I would advocate writing that as

const double *volatile pd;

because it makes it clear that the volatile is related to the pointer, and
follows the natural syntax of the declaration. Also the declaration
specifiers aren't saying that we have a double const (what's that,
something that's very const?), we have a const double. The fact is that I
read C declarations left to right just like everything else. That doesn't
stop me being able to parse a declarator correctly just as I can parse an
expression correctly.
Don't ask me why people still use the exceptional way instead of
wrapping their minds around the canonical form.
I won't ask you about people, but I could ask you why you use the
exceptional, non-canonical form. :-)

Sometimes there is an obvious minimal/simplest form which can be
considered canonical. In this case there isn't. Sometimes there is a
commonly agreed upon form that is considered or indeed defined as
canonical. Here there is no form that is agreed upon, so it is arguable
whether the concept of canonical form is applicable here or if it
is whether it means any more than "preferred style". If there is a
canonical form it would be reasonabe to base a definition on "what best
describes/follows the syntax."
This was and still is
the source of heated discussion that usually generates more heat than
light; I won't dive too deep into this argument...
Also, it may just be the crappy compiler i am using but if I declare
the pointer as a constant, pointing at a volatile buffer,

static const u8 * volatile pBuffer;

I seem to be able to assign the pointer a value at run time. This makes
no sense to me.


There is not const pointer here, just a volatile pointer to a const u8.


Since the pointer is not const it is reasonable to be about to assign a
value to it.

Lawrence

Nov 14 '05 #5
Lawrence Kirby wrote:
On Mon, 10 Jan 2005 19:47:01 +0100, Ulrich Eckhardt wrote:
For variable declarations, the rule is to read the thing from the right
to the left, like "ptr is a pointer to a volatile u8".
Not true in general when arrays or functions are involved. The correct
analysis of a declarator is a syntactic one, similar to the analysis
of an expression. Right to left is misleading, although it works some
of the time.


Right, I should have said 'a simple rule of thumb is'.
So if you want a
volatile pointer to an const double you would use
double const* volatile pd;


I would advocate writing that as

const double *volatile pd;

because it makes it clear that the volatile is related to the
pointer, and follows the natural syntax of the declaration.
Also the declaration specifiers aren't saying that we have a double
const [...], we have a const double.


You are mixing English with C here. There are languages where an attribute
like 'constant' appears after the object that is constant, just because
your language puts it before the object doesn't mean C should.
The fact is that I read C declarations left to right just like everything
else. That doesn't stop me being able to parse a declarator correctly
just as I can parse an expression correctly.


Reading it from right to left is "like a finger pointing at the moon. Once
you see the moon, you don't need the finger any more." IOW, you are one
step closer to understanding C syntax that those that need the mentioned
rule of thumb. ;)
Don't ask me why people still use the exceptional way instead of
wrapping their minds around the canonical form.


I won't ask you about people, but I could ask you why you use the
exceptional, non-canonical form. :-)


Hmmm. If you count all the cases of variable declarations where the CV
qualifier can be on the right side of what it is associated with and the
cases where it can be on its left, you will find there are more cases
where it can be on the right. In fact in every case it can be on the
right, but only in a few cases it can be on the left.

I don't get how you can call the only way that always works
exceptional. ;-)

Uli

Nov 14 '05 #6

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

Similar topics

9
by: Alfonso Morra | last post by:
I have the following data types: typedef union { long l ; double f; char* s ; void* p ; } Value ; typedef struct {
5
by: Alfonso Morra | last post by:
Hi, I am writing a messaging library which will allow me to send a generic message structure with custom "payloads". In many cases, a message must store a non-linear data structure (i.e....
22
by: Assaf | last post by:
hi all i know that i should not cross-post, but i am not sure to which group to post this question. 2 quesions about volatile: 1. i use volatile when 2 threads access the same variable...
3
by: jiing | last post by:
Now I am try to transfer a memory pointer between two threads. but there has a error mesage. _CrtIsValidHeapPointer(pUserData) in dbgheap.c I lookup google and find it's seems to be the local...
6
by: venky | last post by:
Hi all, I'm new to group n c. i want to know in detail about volatile qualifier and pointer to functions with examples. thank you
12
by: Frodo Baggins | last post by:
Hi I need to know the size of the memory block pointed to by a char* in a function receiving this pointer. Typically, this pointer points to a string. strlen() will not do the job since sometimes...
8
by: barcaroller | last post by:
I have a pointer to a memory block. Is there a way I can map a vector<Tto this memory block? I would like to take advantage of the powerful vector<T> member functions. Please note that I cannot...
2
by: kishoremupparaju | last post by:
what is the use of volatile keyword in c language
3
by: Rakesh Kumar | last post by:
Hi - I am actually trying to get my feet in multi-threaded C++ programming. While I am aware that the C++ standard does not talk about threads (at least, for now - in C++03) - my question is more...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...

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.