Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Casting volatile and const variables

Reply
Thread Tools

Casting volatile and const variables

 
 
Ravi
Guest
Posts: n/a
 
      09-24-2011
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
 
Reply With Quote
 
 
 
 
Richard Damon
Guest
Posts: n/a
 
      09-24-2011
On 9/24/11 4:50 PM, Ravi wrote:
> 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).
 
Reply With Quote
 
 
 
 
Ben Bacarisse
Guest
Posts: n/a
 
      09-24-2011
Richard Damon <(E-Mail Removed)> writes:

> On 9/24/11 4:50 PM, Ravi wrote:
>> 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?


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.

--
Ben.
 
Reply With Quote
 
Uno
Guest
Posts: n/a
 
      09-25-2011
On 9/24/2011 2:39 PM, Ben Bacarisse wrote:
> Richard Damon<(E-Mail Removed)> writes:
>
>> On 9/24/11 4:50 PM, Ravi wrote:
>>> 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?

>
> 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.
>


example?
--
uno
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Casting volatile and const variables ramu C Programming 1 10-26-2009 02:29 PM
Casting from const pair<const unsigned char*, size_t>* to constpair<unsigned char*, size_t>* Alex Vinokur C++ 9 10-13-2008 05:05 PM
const vector<A> vs vector<const A> vs const vector<const A> Javier C++ 2 09-04-2007 08:46 PM
Casting int'** to 'const int * const * const' dosn't work, why? Jonas.Holmsten@gmail.com C Programming 11 07-01-2007 06:16 PM
Use of the Volatile keyword for a pointer to a volatile memory block ben C Programming 5 01-11-2005 05:38 PM



Advertisments