volatile ptr question

L

LBJ

I have the following C code:

//pHeadLoc contains an address that can be modified externally,
//therefore it is a volatile pointer to a pointer
volatile int** const pHeadLoc = (int**)0x456000;


int* getHeadPtr()
{
return *pHeadLoc;
}

int readHeadPtr()
{
const int* pHeadPtr;
pHeadPtr = getHeadPtr();
return *pHeadPtr;
}


pHeadLoc is a pointer to a pointer to the head of an array.
pHeadLoc will always be constant, but *pHeadLoc can change without the
CPU's knowledge, as can the rest of the contents of said array. when
readHeadPtr() executes the pHeadPtr = getHeadPtr(); statement, I am
pretty sure that i will be getting valid, up-to-date value since
pHeadLoc is volatile. But when I go to access the data in pHeadPtr, I
am not sure that will be getting valid, up-to-date data since pHeadPtr
is not declared volatile. Does anybody know how this will behave?
email me at (e-mail address removed).
 
J

Jack Klein

I have the following C code:

//pHeadLoc contains an address that can be modified externally,
//therefore it is a volatile pointer to a pointer
volatile int** const pHeadLoc = (int**)0x456000;


int* getHeadPtr()
{
return *pHeadLoc;
}

int readHeadPtr()
{
const int* pHeadPtr;
pHeadPtr = getHeadPtr();
return *pHeadPtr;
}


pHeadLoc is a pointer to a pointer to the head of an array.
pHeadLoc will always be constant, but *pHeadLoc can change without the
CPU's knowledge, as can the rest of the contents of said array. when
readHeadPtr() executes the pHeadPtr = getHeadPtr(); statement, I am
pretty sure that i will be getting valid, up-to-date value since
pHeadLoc is volatile. But when I go to access the data in pHeadPtr, I
am not sure that will be getting valid, up-to-date data since pHeadPtr
is not declared volatile. Does anybody know how this will behave?
email me at (e-mail address removed).

No, post here, read here.

Why can't you just make pHeadPtr volatile?

const volatile int *pHeadPtr = getHeadPtr();

???

Why do you think you need const in a function this small?

Why not simplify the whole thing to:

int readHeadPtr(void)
{
return *(volatile int *)getHeadPtr();
}
 
A

Anupam

I have the following C code:

//pHeadLoc contains an address that can be modified externally,
//therefore it is a volatile pointer to a pointer
volatile int** const pHeadLoc = (int**)0x456000;

The correct definition of pHeadLoc would be ::
int* volatile * const pHeadLoc = (int* volatile *)0x456000;
because the address inside of pHeadLoc is volatile from your
statement.
int* getHeadPtr()
{
return *pHeadLoc;
}

int readHeadPtr()
{
const int* pHeadPtr;
pHeadPtr = getHeadPtr();
return *pHeadPtr;
}


pHeadLoc is a pointer to a pointer to the head of an array.
pHeadLoc will always be constant, but *pHeadLoc can change without the
CPU's knowledge, as can the rest of the contents of said array. when
readHeadPtr() executes the pHeadPtr = getHeadPtr(); statement, I am
pretty sure that i will be getting valid, up-to-date value since
pHeadLoc is volatile. But when I go to access the data in pHeadPtr, I
am not sure that will be getting valid, up-to-date data since pHeadPtr
is not declared volatile. Does anybody know how this will behave?
This is a no go. It is simply a matter of 'Why cant you do just
that?' It will not behave correctly in all cases. Thats for sure.
 

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

Forum statistics

Threads
474,139
Messages
2,570,805
Members
47,355
Latest member
MavoraTech

Latest Threads

Top