检查指针是否允许NULL? [英] checking if pointer is NULL allowed?

查看:68
本文介绍了检查指针是否允许NULL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



只是为了检查,如果我将指针显式设置为NULL,我不允许

取消引用它?为什么会这样,它不是像它指向任何垃圾一样?为什么还要将其设置为NULL。我可以

记住一些实例,其中

i就是这样,而printf报告的内容如下:(null)。


我''我问,因为我在某处读到它无效。我认为

根本无法取消引用

a指针,它不会指向任何东西。


但是我可以检查它是否为NULL或不对吗? (我希望):-)


Rgds,

-alef

Hi,
Just to check, if i set a pointer explicitly to NULL, i''m not allowed
to dereference it? Why is that, it''s not
like it''s pointing to any garbage right? Why else set it to NULL. I can
remember some instances where
i did just that and printf reported something like : (null).

I''m asking because i read somewhere that it is not valid. I thought it
simply wasn''t valid to dereference
a pointer who is _not_ set to point to anything.

But i can check if it is NULL or not right ? ( i hope ) :-)

Rgds,
-alef

推荐答案

在文章< 45 ********************* @ news.xs4all.nl>,

atv< ; al ** @ xs4all.nlwrote:
In article <45*********************@news.xs4all.nl>,
atv <al**@xs4all.nlwrote:

>只是为了检查,如果我将指针显式设置为NULL,我不被允许
取消引用它?
>Just to check, if i set a pointer explicitly to NULL, i''m not allowed
to dereference it?



正确。

Right.


>为什么会这样,它不是像
它指向任何垃圾吗?
>Why is that, it''s not
like it''s pointing to any garbage right?



它没有指向任何东西,所以我想从这个角度来看

它并没有指向任何垃圾。 ;-)

It isn''t pointing to anything, so I guess from that point of view
it isn''t pointing to any garbage. ;-)


>为什么还要将其设置为NULL。我可以记住我做过的一些实例,而printf报告的内容如下:( null)。
>Why else set it to NULL. I can
remember some instances where
i did just that and printf reported something like : (null).


>我在问,因为我在某处读到它无效。我认为
根本无法取消引用
一个指针,它不会被指向任何东西。
>I''m asking because i read somewhere that it is not valid. I thought it
simply wasn''t valid to dereference
a pointer who is _not_ set to point to anything.



但是NULL指针 - 已设置为不指向任何内容。 NULL

指针是-defined-,不指向任何有效对象。

取消引用它的意思是什么?这种情况在哲学上类似于取消引用void *指针 - 没有是那里!

-

没有人有权通过要求经验证据来摧毁另一个人的信念。 - Ann Landers

But a NULL pointer -has- been set not to point to anything. The NULL
pointer is -defined- as not pointing to any valid object. What would
it mean to dereference it? The situation is philosophically similar
to dereferencing a void* pointer -- there is no "is" there!
--
"No one has the right to destroy another person''s belief by
demanding empirical evidence." -- Ann Landers


atv写道:
atv wrote:



只是为了检查,如果我将指针显式设置为NULL,我不允许

取消引用它?
Hi,
Just to check, if i set a pointer explicitly to NULL, i''m not allowed
to dereference it?



你_can_,但行为未定义,因为在C中,NULL指针

保证不指向任何有效对象。

You _can_, but the behaviour is undefined, since in C, a NULL pointer
is guaranteed to not point to any valid object.


为什么会这样,它不是'b $ b'就像它指向任何垃圾一样?为什么还要将其设置为NULL。
Why is that, it''s not
like it''s pointing to any garbage right? Why else set it to NULL.



将未初始化的指针设置为NULL是一种很好的做法。还有一些

函数需要一个空指针。许多编程结构需要一个

空指针作为标记值。许多函数返回一个空指针

作为失败的指示。


< snip>

Setting an uninitialised pointer to NULL is a good practise. Also some
functions require a null pointer. Many programming constructs require a
null pointer as a sentinel value. Many functions return a null pointer
as an indication of failure.

<snip>


我在问,因为我在某处读到它无效。我认为

根本无法解除引用

一个指针被_not_设置为指向任何东西。
I''m asking because i read somewhere that it is not valid. I thought it
simply wasn''t valid to dereference
a pointer who is _not_ set to point to anything.



是。

Yes.


但我可以检查它是否为NULL或不对吗? (我希望):-)
But i can check if it is NULL or not right ? ( i hope ) :-)



是的,你可以。

Yes, you can.


好吧,如果它不是指向任何东西,不能是我的编译器

只是printf outnull。我发誓我以为我已经看到了这个。

如果我可以检查它是否为NULL(我假设你为什么设置

它首先是NULL,就像你说的,好的做法)

它是如何做到这一点的。它是否跟踪记忆分配?

Well, if it is not pointing to anything anyway, couldn''t my compiler
just printf out "null". And i swear i thought i have seen this.
and if i can check for it to be NULL (i''m assuming that''s why you set
it to NULL in the first place, like you said, good practice) how does
it do this. Does it keep track of memory assignments?


这篇关于检查指针是否允许NULL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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