1

Topic: const and others

Question on coercion of types and const, actually, a subject: the link #include <iostream> int main () {const int a = 333; const int * ap = &a; int * p = (int *) ap;//<- it *p = 444; std:: cout <<"*p =" <<*p <<"\n" <<"*ap =" <<a <<"\n" <<"p =" <<p <<"\n" <<"ap =" <<ap <<"\n";} output: *p =444 *ap=333 p = 0x72fdad7ee70c ap = 0x72fdad7ee70c questions: 1. Coercion const in not-const in such a way it UB? (Where to esteem in sense  a link on the standard if it it?) 2. How to one address there can be two - different - values?

2

Re: const and others

Hello, brainy, you wrote: B> 1. Coercion const in not-const in such a way it UB? Yes. To remove  and to modify object it is possible only if the object initially not was constant. That is, for example, such code is admissible and defined: int a = 333; const int * ap = &a; int * p = (int *) ap; *p = 444; B> (where to esteem in sense  a link on the standard if it it?) in the standard also look. In section [dcl.type.cv]. There and your example is. B> 2. How to one address there can be two - different - values? An observable output - not a consequence of that in one cell of storage there are two different values, and a consequence of that the behavior of the program is not defined. (In a reality here the compiler does not look at all that lies in storage under the pointer. He knows that there there was number 333, and he knows that it is impossible to change constants. Therefore does an output that under the pointer ap there can be only number 333, and, actually, deduces number 333).

3

Re: const and others

Hello, brainy, you wrote: B> 1. Coercion const in not-const in such a way it UB? (Where to esteem in sense  a link on the standard if it it?) https://en.cppreference.com/w/cpp/language/const_cast Modifying a const object through a non-const access path and referring to a volatile object through a non-volatile glvalue results in undefined behavior. const int j = 3;//j is declared const int* pj = const_cast <int *> (&j);//*pj = 4;//undefined behavior

4

Re: const and others

Hello, brainy, you wrote: B> a question on coercion of types and const, actually, a subject: B> link B> B>#include <iostream> B> int main () B> {B> const int a = 333; B> const int * ap = &a; B> int * p = (int *) ap;//<- it B> *p = 444; B> std:: cout <<"*p =" <<*p <<"\n" <<"*ap =" <<a <<"\n" B> <<"p =" <<p <<"\n" <<"ap =" <<ap <<"\n"; B>} B> B> output: B> *p =444 B> *ap=333 B> p = 0x72fdad7ee70c B> ap = 0x72fdad7ee70c B> questions: B> 1. Coercion const in not-const in such a way it UB? (Where to esteem in sense  a link on the standard if it it?) B> 2. How to one address there can be two - different - values? The answer to the second question - at you a misprint in the code: "*ap =" <<a <<"\n" After correction all becomes normal without everyone UB.

5

Re: const and others

Hello, uentity, you wrote: U> the Answer to the second question - at you a misprint in the code: "*ap =" <<a <<"\n" U> After correction all becomes normal without everyone UB. Well is not present, does not become. Undefined behavior remains.

6

Re: const and others

Hello, brainy, you wrote: B> questions: B> 1. Coercion const in not-const in such a way it UB? (Where to esteem in sense  a link on the standard if it it?) B> 2. How to one address there can be two - different - values? 1) search in a site  2) Not the link to the standard, however it is obvious, but  an explanation of a subject which Author is pleasant personally to me http://rsdn.org/forum/cpp/2644701: Erop Date: 04.09.07 3) And what you wanted to make actually? Speech about a program error which is unexpected for you was showed, or about any idea , which ?