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

volatile const

Hi Everyone,

The following code compiles without any error,

int main()
{
volatile const int a = 10;
return(0);
};

But i expected a compilation error as a is being told as a constant
and at the same time indicating that its value could change outside
the program's control...

Why isn't this an error?

Thanks in advance ! ! !
Apr 10 '08 #1
6 2370
Rahul wrote:
The following code compiles without any error,

int main()
{
volatile const int a = 10;
return(0);
};

But i expected a compilation error as a is being told as a constant
and at the same time indicating that its value could change outside
the program's control...

Why isn't this an error?
Why should it be an error? This is exactly what 'volatile const' is
supposed to mean in the first place: the program is not allowed to
change the object, but its value can change "by itself".

--
Best regards,
Andrey Tarasevich
Apr 10 '08 #2
Rahul <sa*****@yahoo.co.inwrote:
int main()
{
volatile const int a = 10;
return(0);
};

But i expected a compilation error as a is being told as a constant
No, it's being declared as something _you_ won't change.
and at the same time indicating that its value could change outside
the program's control...
Quite. It is something your code won't change, but something else will.
One example of this could be a hardware counter, or an input port.

Richard
Apr 10 '08 #3
Rahul wrote:
Hi Everyone,

The following code compiles without any error,

int main()
{
volatile const int a = 10;
return(0);
};

But i expected a compilation error as a is being told as a constant
and at the same time indicating that its value could change outside
the program's control...

Why isn't this an error?
Because for some stupid reason in C const doesn't mean 'constant' but 'read
only'

Bye,Jojo
Apr 10 '08 #4
In article <ft**********@aioe.org>,
Andrey Tarasevich <an**************@hotmail.comwrote:
> volatile const int a = 10;
>Why should it be an error? This is exactly what 'volatile const' is
supposed to mean in the first place: the program is not allowed to
change the object, but its value can change "by itself".
The standard gives this example:

extern const volatile int real_time_clock;

You can't change the time, but it changes anyway.

-- Richard
--
:wq
Apr 10 '08 #5
Hello Joachim,
Because for some stupid reason in C const doesn't mean 'constant' but
'read only'
Yes, absolutely. Furthermore, "const" means read only JUST for you. On the
other hand, "volatile" doesn't mean you can modify it but you HAVE TO reload
it from memory every time you use it. "volatile" is more like an indicator
for compiler to protect from optimizing.

raof01
"Thoughts are but dreams till their effects be tried. -- William Shakespeare"
Jun 27 '08 #6
"raof01" <ra****@gmail.comwrote in message
news:7c*************************@news.cn99.com...
Hello Joachim,
>Because for some stupid reason in C const doesn't mean 'constant' but
'read only'

Yes, absolutely. Furthermore, "const" means read only JUST for you. On the
other hand, "volatile" doesn't mean you can modify it but you HAVE TO
reload it from memory every time you use it. "volatile" is more like an
indicator for compiler to protect from optimizing.
Its not clear at what level a compiler will stop optimizing; its all
implementation defined. It seems like link-time optimizations can mess
around with volatiles if they can obtain certain levels of information... I
think smart-a$@ compilers think they can reorder something, and ultimately
end up generating phantom behavior under the random nature of various
race-conditions. This is especially relevant when the 'volatile' keyword is
used in conjunction with multi-threaded algorithms...

Jun 27 '08 #7

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

Similar topics

4
by: newsock | last post by:
Why need to qualify a member function "volatile"? Why need to qualify a object "volatile"? When need to "const_cast" away "volatile" of an object and a member function? I also saw some code...
4
by: dwaach | last post by:
Hi, I have something like. struct X {}; X ox; X* pox=&ox; X*& volatile r =pox;
17
by: Radde | last post by:
HI, Can volatile variables be accessed by many processess..or only one process can access it.. Cheers..
2
by: LBJ | last post by:
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; ...
1
by: Mantorok Redgormor | last post by:
strictly speaking we can't say that const means "read only" but instead that it means "do not modify me please" since an object can be both modifiable and not modifiable or volatile and const,...
5
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...
9
by: mlimber | last post by:
I am using placement new to locate a shared data structure at a particular location in shared memory. The problem is that when I access the data on one of the two processors sharing it, I don't get...
9
by: d.f.s. | last post by:
In the post below, 'copy constructor?', the answers refer to an object declared as const volatile. Now I'm confused. Are those terms not mutually exclusive? const='Hey compiler! This is not...
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...
3
by: C++Liliput | last post by:
It seems that the keyword "volatile" is used to make sure that threads reading (or writing to) the same data should see a consistent picture of the variable i.e. updates made to the common data...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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:
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:
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...
0
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,...

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.