是UB一旦是L值吗? [英] Is it UB once it's an L-value?

查看:72
本文介绍了是UB一旦是L值吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



以下代码有什么问题?:

#include< cstdlib>


int main()

{

for(unsigned i = 0; i!= 1000; ++ i)

{

int * p = reinterpret_cast< int *>(std :: rand());

}

}

在代码中,我'创建一个值无效的L值指针。

但是,永远不会访问无效的内存地址。


程序是否显示未定义的行为?


-Tomás


Anything wrong with the following code?:
#include <cstdlib>

int main()
{
for (unsigned i = 0; i != 1000; ++i)
{
int *p = reinterpret_cast<int*>( std::rand() );
}
}
In the code, I''m creating an L-value pointer whose value is invalid.
However, the invalid memory address is never accessed.

Does the program exhibit Undefined Behaviour?

-Tomás

推荐答案

2006-05-28 17:22,Tomás写道:
On 2006-05-28 17:22, Tomás wrote:
以下代码有什么问题?:

#include< cstdlib>

int main()
{
for (无符号i = 0; i!= 1000; ++ i)
{
int * p = reinterpret_cast< int *>(std :: rand());
}
}

在代码中,我正在创建一个L值指针,其值无效。
H无论如何,永远不会访问无效的内存地址。

程序是否显示未定义的行为?
Anything wrong with the following code?:
#include <cstdlib>

int main()
{
for (unsigned i = 0; i != 1000; ++i)
{
int *p = reinterpret_cast<int*>( std::rand() );
}
}
In the code, I''m creating an L-value pointer whose value is invalid.
However, the invalid memory address is never accessed.

Does the program exhibit Undefined Behaviour?




不知道为什么,如果只是有一个无效的指针足以导致UB

然后以下代码将是一个问题:


int main()

{

int * p;

返回0;

}


Erik Wikstr?m < br $> b $ b -

我一直希望我的电脑和我的电话一样好用。我的愿望实现了,因为我再也无法想象如何使用我的电话了。 - Bjarne Stroustrup



Can''t see why, if just having an invalid pointer was enough to cause UB
then the following code would be a problem:

int main()
{
int *p;
return 0;
}

Erik Wikstr?m
--
"I have always wished for my computer to be as easy to use as my
telephone; my wish has come true because I can no longer figure
out how to use my telephone" -- Bjarne Stroustrup


Tomás写道:
Tomás wrote:
int * p = reinterpret_cast< int *>(std :: rand( ));
程序是否显示未定义的行为?
int *p = reinterpret_cast<int*>( std::rand() ); Does the program exhibit Undefined Behaviour?




是的,并在硬件中绘制结果将说明原因。

我可以设想一个CPU可以有效地将指针存储在特殊的

寄存器中,如果位模式复制到这样的寄存器中则会出现故障

没有引用有效的记忆。所以编译器必须乐观地编译你的p

,并使用这个寄存器,即使你从不取消引用p。


这是访问删除指针的原因之一未定义。


如果一个An寄存器有一个奇数,我认为摩托罗拉68000有故障...


-

Phlip
http://c2.com/cgi/wiki ?ZeekLand < - 不是博客!!!



Yes, and sketching the result in hardware will illustrate why.

I can conceive of a CPU that efficiently stores pointers in special
registers, and which faults if a bit pattern copies into such a register
that doesn''t refer to valid memory. So a compiler must compile your p
optimistically, and use this register even though you never dereference p.

This is among the reasons why accessing a deleted pointer is undefined.

I think the Motorola 68000 faults if an An register gets an odd number...

--
Phlip
http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!!


Erik Wikstr?m写道:
Erik Wikstr?m wrote:
不知道为什么,如果只是有一个无效的指针足以导致UB
那么下面的代码将是一个问题:
int * p;
Can''t see why, if just having an invalid pointer was enough to cause UB
then the following code would be a problem: int *p;




您没有复制任何值。所以在我的寄存器示例中,编译器保留了

寄存器并且没有更改其当前值。所以周围的操作码

必须保持这个价值健康。


-

Phlip
http://c2.com/cgi/wiki?ZeekLand < - 不是博客! !!



You copied no value in. So in my register example, the compiler reserved the
register and did not change its current value. So the surrounding opcodes
must keep that value healthy.

--
Phlip
http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!!


这篇关于是UB一旦是L值吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆