avatar
question for C++ constant# Programming - 葵花宝典
n*d
1
Can anybody explain it?
ip=0x22cce4 &k=0x22cce4
*ip=5
ip=0x22cce4 &k=0x22cce4
*ip=6
k=5
#include
using namespace std;
int main() {
const int k = 5;
long k_add = (long)&k;
int *ip = (int *)k_add;
cout << "ip=" << ip << " &k=" << &k << endl;
cout << "*ip=" << *ip << endl;
*ip = 6;
cout << "ip=" << ip << " &k=" << &k << endl;
cout << "*ip=" << *ip << endl;
cout << "k=" << k << endl;
}
avatar
t*t
2
it's illegal (undefined) to change a const

【在 n**d 的大作中提到】
: Can anybody explain it?
: ip=0x22cce4 &k=0x22cce4
: *ip=5
: ip=0x22cce4 &k=0x22cce4
: *ip=6
: k=5
: #include
: using namespace std;
: int main() {
: const int k = 5;

avatar
n*d
3
Yes, it is undefined from standard view. However, the complier lets it go
and generates the different result, which means compiler generates some code
for it. I am curious what compiler does.

【在 t****t 的大作中提到】
: it's illegal (undefined) to change a const
avatar
a*a
4
Why would you like to concern with compiler errors? Are you a compiler
doctor?
Since the overwhelming capacity of logic in reality is MUCH MUCH larger than
any kind of programs can be devised by human being, there are infinite
corners
a program (including a compiler) would give up to cope with.
Particular to your question, undefined behavior means an implement of the
compiler could do whatever it likes to. Without specific information on
which compiler you are talking about, it is really not a g

【在 n**d 的大作中提到】
: Yes, it is undefined from standard view. However, the complier lets it go
: and generates the different result, which means compiler generates some code
: for it. I am curious what compiler does.

avatar
t*t
5
generates the "different" result, different with what? different with what
you expect? you should expect *anything*, since under the condition of
undefined, anything is correct.
it's not beneficial for you to know what compiler does, especially at your
level.

code

【在 n**d 的大作中提到】
: Yes, it is undefined from standard view. However, the complier lets it go
: and generates the different result, which means compiler generates some code
: for it. I am curious what compiler does.

avatar
a*a
6
Exactly.

【在 t****t 的大作中提到】
: generates the "different" result, different with what? different with what
: you expect? you should expect *anything*, since under the condition of
: undefined, anything is correct.
: it's not beneficial for you to know what compiler does, especially at your
: level.
:
: code

avatar
n*d
7
TIC: chapter 8
Legal but bad practice
//: C08:PointerAssignment.cpp
int d = 1;
const int e = 2;
int* u = &d; // OK -- d not const
//! int* v = &e; // Illegal -- e const
int* w = (int*)&e; // Legal but bad practice
int main() {} ///:~

【在 a**a 的大作中提到】
: Exactly.
avatar
a*a
8
Good for you. Concentrate on text and you will learn more.

【在 n**d 的大作中提到】
: TIC: chapter 8
: Legal but bad practice
: //: C08:PointerAssignment.cpp
: int d = 1;
: const int e = 2;
: int* u = &d; // OK -- d not const
: //! int* v = &e; // Illegal -- e const
: int* w = (int*)&e; // Legal but bad practice
: int main() {} ///:~

avatar
t*t
9
quote my self:
it is illegal to *change* a const object
end quote

【在 n**d 的大作中提到】
: TIC: chapter 8
: Legal but bad practice
: //: C08:PointerAssignment.cpp
: int d = 1;
: const int e = 2;
: int* u = &d; // OK -- d not const
: //! int* v = &e; // Illegal -- e const
: int* w = (int*)&e; // Legal but bad practice
: int main() {} ///:~

avatar
n*d
10
Is it shame to ask why if 2 compilers generate the same result? AndOne of them is gcc 3
.4.4! I am confident my learning ability. I appreciate if you can say sth
for the reason.
学而不思则罔,思而不学则殆

【在 a**a 的大作中提到】
: Good for you. Concentrate on text and you will learn more.
avatar
a*a
11
I am really sorry that you are lost in the details and forget the big
picture.
The big picture is, any compiler is just an implementation of the standard.
All implementations are trying to follow the standard. So people only want
to talk about standard. Compilers are always in transition by all sort of
technical or nontechnical reasons. If you indulge yourself digging dark
corners of compilers, you are really wasting your time in something unuseful.
You should use compiler as a tool, not a refer

【在 n**d 的大作中提到】
: Is it shame to ask why if 2 compilers generate the same result? AndOne of them is gcc 3
: .4.4! I am confident my learning ability. I appreciate if you can say sth
: for the reason.
: 学而不思则罔,思而不学则殆

avatar
z*8
12
其实原因很简单,跟便一起的优化有关。
k本身是声明为integer constant expression,因而被编译器看作常数,但你使用它的
值的时候,便一起直接使用5,而不是到内存中看。也就是汇编里的立即寻址。
内存中真正的值应该是6。
这个行为确实是未定义的,大部分编译器之所以选择这样做是为了更好的优化性能。
avatar
f*Q
13
精神可嘉。
avatar
n*d
14
That is what I want to know. Thanks!

【在 z********8 的大作中提到】
: 其实原因很简单,跟便一起的优化有关。
: k本身是声明为integer constant expression,因而被编译器看作常数,但你使用它的
: 值的时候,便一起直接使用5,而不是到内存中看。也就是汇编里的立即寻址。
: 内存中真正的值应该是6。
: 这个行为确实是未定义的,大部分编译器之所以选择这样做是为了更好的优化性能。

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。