由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - C++ 的 问题
相关主题
question for C++ constant一个C++语法问题
一个诡异的const_cast问题how do I reseat a reference?
question about c++ constructorprivate destructor
请问这个C++程序有什么问题吗C++ question
conversion between const to nonconst谁来解释一下这个是compiler问题吗?
关于const_cast,地址一样,值不同?c++标准函数传递一问
g++ default optimization errora question about CAST
member and friend问题: C++ static_cast between int and float
相关话题的讨论汇总
话题: undefined话题: int话题: const话题: array话题: 0012fed4
进入Programming版参与讨论
1 (共1页)
c**a
发帖数: 316
1
const int i = 5;
const int* p = &i;
int *q = const_cast(p);
*q =4;
int array[i];
那么 i 是 多少, array 长度是多少?
c***a
发帖数: 655
2
你找个机器编译一下试试不就知道了。

【在 c**a 的大作中提到】
: const int i = 5;
: const int* p = &i;
: int *q = const_cast(p);
: *q =4;
: int array[i];
: 那么 i 是 多少, array 长度是多少?

t****t
发帖数: 6806
3
哦, 看错了, 以为你用的q
那么, 结果是undefined

【在 c***a 的大作中提到】
: 你找个机器编译一下试试不就知道了。
c**a
发帖数: 316
4
为啥是 undefined?
compile 不可能查出来 q 改了 i 的值。。。
array[i] 的 i 必须是 complie time known, 那时 是 5
所以 ,i 是 4, array 长 5.

【在 t****t 的大作中提到】
: 哦, 看错了, 以为你用的q
: 那么, 结果是undefined

t****t
发帖数: 6806
5
改const就是undefined, 程序某处undefined那整个程序都undefined

【在 c**a 的大作中提到】
: 为啥是 undefined?
: compile 不可能查出来 q 改了 i 的值。。。
: array[i] 的 i 必须是 complie time known, 那时 是 5
: 所以 ,i 是 4, array 长 5.

c**a
发帖数: 316
6
强。。。

【在 t****t 的大作中提到】
: 改const就是undefined, 程序某处undefined那整个程序都undefined
L*******r
发帖数: 1011
7
hehe, 你还在这。

【在 t****t 的大作中提到】
: 哦, 看错了, 以为你用的q
: 那么, 结果是undefined

T*****9
发帖数: 2484
8
const char *p进行(char*)p好像也是undefined behavior?

【在 t****t 的大作中提到】
: 哦, 看错了, 以为你用的q
: 那么, 结果是undefined

t****t
发帖数: 6806
9
这个好象是可以的

【在 T*****9 的大作中提到】
: const char *p进行(char*)p好像也是undefined behavior?
g*********8
发帖数: 53
10
why it is undefined?
I can run the program.
but when I print like this
{{{
...
cout<<*q<<" "< cout< }}}
the output is:
4 0012FED4
5 0012FED4
what is the reason?
f******y
发帖数: 2971
11
it seems compiler generated an hidden variable for *p. That is, *p is no longer equal to the value stored at p.
btw, 你机器的内存似乎不大 :D

【在 g*********8 的大作中提到】
: why it is undefined?
: I can run the program.
: but when I print like this
: {{{
: ...
: cout<<*q<<" "<: cout<: }}}
: the output is:
: 4 0012FED4

t****t
发帖数: 6806
12
undefined has nothing to do with can or can not run.
in fact, most undefined program can be compiled and runned. if it can't pass
compile, it is called "ill-formed".

【在 g*********8 的大作中提到】
: why it is undefined?
: I can run the program.
: but when I print like this
: {{{
: ...
: cout<<*q<<" "<: cout<: }}}
: the output is:
: 4 0012FED4

1 (共1页)
进入Programming版参与讨论
相关主题
问题: C++ static_cast between int and floatconversion between const to nonconst
[合集] 请问-fno-implicit-templates的用处关于const_cast,地址一样,值不同?
问一个for循环的问题g++ default optimization error
namespace defined in another filemember and friend
question for C++ constant一个C++语法问题
一个诡异的const_cast问题how do I reseat a reference?
question about c++ constructorprivate destructor
请问这个C++程序有什么问题吗C++ question
相关话题的讨论汇总
话题: undefined话题: int话题: const话题: array话题: 0012fed4