由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - question for C++ constant
相关主题
C++ 的 问题谁来解释一下这个是compiler问题吗?
g++ default optimization error问一个for循环的问题
一个C++语法问题namespace defined in another file
how do I reseat a reference?member and friend
question about c++ constructorOne c++ non-type template question
private destructora simple question about constructor
请问这个C++程序有什么问题吗C array
An interesting C++ compile errorWhat is wrong in this array declaration.
相关话题的讨论汇总
话题: ip话题: int话题: 0x22cce4话题: endl话题: compiler
进入Programming版参与讨论
1 (共1页)
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
13
精神可嘉。
n**d
发帖数: 9764
14
That is what I want to know. Thanks!

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

1 (共1页)
进入Programming版参与讨论
相关主题
What is wrong in this array declaration.question about c++ constructor
问个虚函数的作用private destructor
A C++ compiler related interview question请问这个C++程序有什么问题吗
a[i]=i++An interesting C++ compile error
C++ 的 问题谁来解释一下这个是compiler问题吗?
g++ default optimization error问一个for循环的问题
一个C++语法问题namespace defined in another file
how do I reseat a reference?member and friend
相关话题的讨论汇总
话题: ip话题: int话题: 0x22cce4话题: endl话题: compiler