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
|