Casting volatile and const variables

R

Ravi

Hello friends~

I read on the internet,
that const and volatile
variable should not be
type casted. Can you
please tell me why
shouldn't we type cast
const and volatile
variables?

Regards
 
R

Richard Damon

Hello friends~

I read on the internet,
that const and volatile
variable should not be
type casted. Can you
please tell me why
shouldn't we type cast
const and volatile
variables?

Regards

The danger with casting const/volatile variables is that writing to a
object that is const is undefined behavior, as is accessing a volatile
object except as a volatile object.

If you have a variable of type "pointer to const x", it is ok to cast
that to "pointer to x" and write through it, if the pointer actually
points to a object that isn't const (you are allowed to safely place the
address of a non const object into a pointer to const variable).
 
B

Ben Bacarisse

An example might help. Can you show some code that you think is covered
by this rule? I ask, because as you have it worded, I don't see any
problem at all.
The danger with casting const/volatile variables is that writing to a
object that is const is undefined behavior, as is accessing a volatile
object except as a volatile object.

Agreed if the first 8 words are deleted! The trouble is that a cast on
a cont variable does no let you modify it, nor done one on a volatile
variable lets you access it as anything than the volatile type it is. I
think you've corrected the OP's question to answer the they probably
should have asked.
If you have a variable of type "pointer to const x", it is ok to cast
that to "pointer to x" and write through it, if the pointer actually
points to a object that isn't const (you are allowed to safely place
the address of a non const object into a pointer to const variable).

Also agreed, but none of that has anything to do with the what OP asked!

As I said, it may be very close to what the OP wanted to ask. Maybe they
read something about "casting away const" from the target type of a
pointer and turned that into "casting a const variable". But it's also
possible that they read something quite wrong. I'd like to see an
example first.
 
U

Uno

An example might help. Can you show some code that you think is covered
by this rule? I ask, because as you have it worded, I don't see any
problem at all.


Agreed if the first 8 words are deleted! The trouble is that a cast on
a cont variable does no let you modify it, nor done one on a volatile
variable lets you access it as anything than the volatile type it is. I
think you've corrected the OP's question to answer the they probably
should have asked.


Also agreed, but none of that has anything to do with the what OP asked!

As I said, it may be very close to what the OP wanted to ask. Maybe they
read something about "casting away const" from the target type of a
pointer and turned that into "casting a const variable". But it's also
possible that they read something quite wrong. I'd like to see an
example first.

example?
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top