NULL指针解除引用 - 行为 [英] NULL pointer dereferencing - behaviour

查看:126
本文介绍了NULL指针解除引用 - 行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C ++标准是否定义了在NULL指针

解除引用的情况下应该发生什么。如果没有,是否说它是非法的?


如果是的话,它会说出来吗?

解决方案

如果我取消引用这样的结构指针怎么办?


SturctPtr->会员;


如果StructPtr为NULL并且成员不是

结构中的第一个,那么会发生什么情况,即它从一开始就是4字节的对齐?


BigMan写道:

C ++标准是否定义了在NULL指针取消引用的情况下应该发生什么。如果没有,它是否说它是非法的?




我知道在C ++中几乎不推荐使用NULL。


Stroustrup建议偏好0,因为没有在

地址分配对象0.


此外,我不认为存在标准关于Null的行为

指针引用。


这是在运行时发生的事情而且C ++编译器没有

控制它(恕我直言)。


PS:


为什么在地球上一个人应该尝试取消引用(有意识)a br />
空指针???


再见


Andrea


< blockquote> BigMan写道:

如果我取消引用这样的结构指针怎么办:

SturctPtr->会员;

应该怎么做?如果StructPtr为NULL并且Member不是
结构中的第一个,即它从一开始就是4个字节,则会发生吗?




解除引用空指针的结果是_undefined_。 IOW,任何事情都可能发生,并且允许发生AFA C ++。最现代的

操作系统将指示某种特殊情况,并且
中止违规计划。但这并不能保证。


V


Does the C++ standard define what should happen in case of NULL pointer
dereferencing. If not, does it say that it is illegal?

Where, if so, does it say it?

解决方案

And what if I dereference a pointer to a structure like this:

SturctPtr->Member;

What should happen if StructPtr is NULL and Member is not first in the
structure, i.e. it is alligned at 4 bytes from the beginning?


BigMan wrote:

Does the C++ standard define what should happen in case of NULL pointer
dereferencing. If not, does it say that it is illegal?



I know that in C++ the use of NULL is almost deprecated.

Stroustrup suggests to prefer 0, because no object is allocated at the
address 0.

Moreover, I don''t think that exists a standard behaviour about Null
pointer deferencing.

It''s something that happens at run time and a C++ compiler has no
control on it (IMHO).

P.S:

Why on the Earth a person should try to dereference (with awareness) a
NULL pointer ???

Bye

Andrea


BigMan wrote:

And what if I dereference a pointer to a structure like this:

SturctPtr->Member;

What should happen if StructPtr is NULL and Member is not first in the
structure, i.e. it is alligned at 4 bytes from the beginning?



The result of dereferencing a null pointer is _undefined_. IOW, anything
can happen and is allowed to happen AFA C++ is concerned. Most modern
operating systems will indicate some kind of exceptional situation and
abort the offending program. But that''s not guaranteed.

V


这篇关于NULL指针解除引用 - 行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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