Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #10885 > unrolled thread
| Started by | Ravi <nospam@null.invalid> |
|---|---|
| First post | 2011-09-24 20:50 +0000 |
| Last post | 2011-09-24 21:41 -0700 |
| Articles | 4 — 4 participants |
Back to article view | Back to comp.lang.c
Casting volatile and const variables Ravi <nospam@null.invalid> - 2011-09-24 20:50 +0000
Re: Casting volatile and const variables Richard Damon <news.x.richarddamon@xoxy.net> - 2011-09-24 17:19 -0400
Re: Casting volatile and const variables Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-09-24 22:39 +0100
Re: Casting volatile and const variables Uno <merrilljensen@q.com> - 2011-09-24 21:41 -0700
| From | Ravi <nospam@null.invalid> |
|---|---|
| Date | 2011-09-24 20:50 +0000 |
| Subject | Casting volatile and const variables |
| Message-ID | <j5lfqm$qap$1@speranza.aioe.org> |
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
[toc] | [next] | [standalone]
| From | Richard Damon <news.x.richarddamon@xoxy.net> |
|---|---|
| Date | 2011-09-24 17:19 -0400 |
| Message-ID | <j5lhge$gs4$1@dont-email.me> |
| In reply to | #10885 |
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).
[toc] | [prev] | [next] | [standalone]
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Date | 2011-09-24 22:39 +0100 |
| Message-ID | <0.b14247b13d258b6773bd.20110924223948BST.87fwjlsjq3.fsf@bsb.me.uk> |
| In reply to | #10886 |
Richard Damon <news.x.richarddamon@xoxy.net> 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.
[toc] | [prev] | [next] | [standalone]
| From | Uno <merrilljensen@q.com> |
|---|---|
| Date | 2011-09-24 21:41 -0700 |
| Message-ID | <9e7m5gFbvU1@mid.individual.net> |
| In reply to | #10888 |
On 9/24/2011 2:39 PM, Ben Bacarisse wrote: > Richard Damon<news.x.richarddamon@xoxy.net> 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
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.c
csiph-web