n**d 发帖数: 9764 | 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;
} |
t****t 发帖数: 6806 | 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;
|
n**d 发帖数: 9764 | 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
|
a**a 发帖数: 416 | 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.
|
t****t 发帖数: 6806 | 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.
|
a**a 发帖数: 416 | 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
|
n**d 发帖数: 9764 | 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.
|
a**a 发帖数: 416 | 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() {} ///:~
|
t****t 发帖数: 6806 | 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() {} ///:~
|
n**d 发帖数: 9764 | 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.
|
a**a 发帖数: 416 | 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. : 学而不思则罔,思而不学则殆
|
z********8 发帖数: 1 | 12 其实原因很简单,跟便一起的优化有关。
k本身是声明为integer constant expression,因而被编译器看作常数,但你使用它的
值的时候,便一起直接使用5,而不是到内存中看。也就是汇编里的立即寻址。
内存中真正的值应该是6。
这个行为确实是未定义的,大部分编译器之所以选择这样做是为了更好的优化性能。 |
f*****Q 发帖数: 1912 | |
n**d 发帖数: 9764 | 14 That is what I want to know. Thanks!
【在 z********8 的大作中提到】 : 其实原因很简单,跟便一起的优化有关。 : k本身是声明为integer constant expression,因而被编译器看作常数,但你使用它的 : 值的时候,便一起直接使用5,而不是到内存中看。也就是汇编里的立即寻址。 : 内存中真正的值应该是6。 : 这个行为确实是未定义的,大部分编译器之所以选择这样做是为了更好的优化性能。
|