为什么这不会失败? [英] Why does this not fail?

查看:69
本文介绍了为什么这不会失败?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

即使a,在下面的赋值中为NULL,此赋值不会导致任何AV:


SOME_PTR * someVar =(SOME_PTR *)a-> b;

但是像这样的东西会导致AV,因为someVar是NULL:


if(someVar-> someType == 1)

{


}

为什么第一次分配不会导致任何访问冲突?

解决方案

文章< 11 *** *******************@74g2000cwt.googlegroups。 com>,

< sg **** @ gmail.comwrote:


>即使a在下面的赋值中为NULL,此赋值不会导致任何AV:


> SOME_PTR * someVar =(SOME_PTR *)a - > b;


>但是像这样的东西会导致AV,因为someVar是NULL:


> if(someVar-> someType == 1)

}


>为什么第一次分配不会导致任何访问冲突?



Chance。


去除空指针只会导致访问违规

当你很幸运。其余的时间,它会做一些或其他通常更难检测到的事情。

-

这很重要要记住,当涉及到法律时,电脑

永远不会复制,只有人类才能复制。计算机给出了

命令,而非许可。只有人才能获得许可。

- Brad Templeton


sg **** @ gmail.com 写道:


即使a,在下面的赋值中为NULL,此赋值不会导致任何AV:


SOME_PTR * someVar =(SOME_PTR *)a-> b;


但是像这样的东西会导致AV,因为someVar是NULL:


if(someVar-> someType == 1)

{


}


为什么第一个分配不会导致任何访问冲突?



无论哪种方式,行为都是未定义的,所以实际上任何事情都是允许发生的。但我想你真正的问题是为什么

未定义的行为以这种方式表现出来。我的第一个想法是

以前的代码实际上并没有对它获得的值进行任何处理,所以编译器可能正在优化它

完全没有取消引用指针。

-

我看到它的方式,一个不同意我的聪明人是

可能是我将在任何给定的

日与之互动的最重要人物。

--Billy Chambless


我认为既然我们没有访问NULL内存,我们将得到b的地址

,即使a是a。是NULL。


怎么样:


&(((type *)0)-field)


这里也没有问题。我还没有得到满意的答复。


Even if "a" is NULL in the assignment below, this assignment does not
cause any AV:

SOME_PTR * someVar = (SOME_PTR *) a->b;
But something like this will cause an AV because "someVar" is NULL:

if (someVar->someType == 1)
{

}
Why does the first assignment not cause any access violation?

解决方案

In article <11**********************@74g2000cwt.googlegroups. com>,
<sg****@gmail.comwrote:

>Even if "a" is NULL in the assignment below, this assignment does not
cause any AV:

>SOME_PTR * someVar = (SOME_PTR *) a->b;

>But something like this will cause an AV because "someVar" is NULL:

>if (someVar->someType == 1)
{
}

>Why does the first assignment not cause any access violation?

Chance.

Derefencing a NULL pointer only results in an access violation
when you are lucky. The rest of the time, it does something or other
that is usually much harder to detect.
--
"It is important to remember that when it comes to law, computers
never make copies, only human beings make copies. Computers are given
commands, not permission. Only people can be given permission."
-- Brad Templeton


sg****@gmail.com writes:

Even if "a" is NULL in the assignment below, this assignment does not
cause any AV:

SOME_PTR * someVar = (SOME_PTR *) a->b;
But something like this will cause an AV because "someVar" is NULL:

if (someVar->someType == 1)
{

}
Why does the first assignment not cause any access violation?

Either way, the behavior is undefined, so anything is actually
allowed to happen. But I suppose your real question is why the
undefined behavior manifests this way. My first thought is that
the former code doesn''t actually do anything with the value that
it obtains, so the compiler is probably optimizing it out
entirely, not dereferencing the pointer at all.
--
"The way I see it, an intelligent person who disagrees with me is
probably the most important person I''ll interact with on any given
day."
--Billy Chambless


I think since we are not accessing NULL memory, we will get the address
of "b", even if "a" is NULL.

What about this:

&( ((type *)0) -field)

There is no problem here too. I am yet to get a satisfactory answer.


这篇关于为什么这不会失败?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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