473,382 Members | 1,692 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,382 software developers and data experts.

volatile ptr question

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 LB****@yahoo.com.
Nov 14 '05 #1
2 2503
On 10 Feb 2004 12:30:24 -0800, lb****@yahoo.com (LBJ) wrote in
comp.lang.c:
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 LB****@yahoo.com.


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();
}

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #2
lb****@yahoo.com (LBJ) wrote in message news:<62*************************@posting.google.c om>...
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. email me at LB****@yahoo.com.

Nov 14 '05 #3

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

Similar topics

4
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...
17
by: Radde | last post by:
HI, Can volatile variables be accessed by many processess..or only one process can access it.. Cheers..
11
by: srinivas reddy | last post by:
Hi, Is there any chance that a program doesn' work properly even after a variable is declared as volatile? I remember somebody mentioning a scenario involving L1, L2 caches. Could anybody throw...
9
by: Tim Rentsch | last post by:
I have a question about what ANSI C allows/requires in a particular context related to 'volatile'. Consider the following: volatile int x; int x_remainder_arg( int y ){ return x % y; }
8
by: Tim Rentsch | last post by:
Here's another question related to 'volatile'. Consider the following: int x; void foo(){ int y; y = (volatile int) x;
14
by: Ian Pilcher | last post by:
It's pretty common to see declarations such as: static volatile sig_atomic_t caught_signal = 0; C99 defines sig_atomic_t as a "... (possibly volatile-qualified) integer type of an object that...
14
by: google-newsgroups | last post by:
Hello, even (or because?) reading the standard (ISO/IEC 9899/1999) I do not understand some issues with volatile. The background is embedded programming where data is exchanged between main...
10
by: Lau Lei Cheong | last post by:
Hello, I really need to use volatile System.Int64 for a .NET v1.1 program in C#. But the compiler complains "a volatile field can not be of type long". How to work around it? Or is there any...
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...
16
by: Ark Khasin | last post by:
I have a memory-mapped peripheral with a mapping like, say, struct T {uint8_t read, write, status, forkicks;}; If I slap a volatile on an object of type struct T, does it guarantee that all...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...

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.