NULL指针初始化 [英] NULL pointer initialization

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

问题描述

全部,


阅读常见问题解答(问题5.2)之后,我看到:


一个带有值的整数常量表达式0"在指针上下文中,
在编译时转换为空指针。常见问题继续说:


char * p = 0;

if(p!= 0)


是有效的事情。


这是C99标准的新变化吗?我记得过去的问题

要求指针始终用NULL而不是0初始化,因为

一些实现中的NULL不一定是(void *)0。 (或者其他什么

沿着我认为的那些方向)


有点偏离主题,但我也相信这现在与C ++如何一致>
标准也适用吗?


如果有任何关于何时发生这种变化的提示,那么什么标准

包含这种新行为等等。


非常感谢,

James Brown

解决方案

文章< Oc********************@pipex.net>,

James Brown< no*@home.netwrote:


>阅读常见问题解答(问答5.2)后,我看到:


> an" ;值为0的整数常量表达式在指针上下文中,在编译时将其转换为空指针。


>这是C99标准的新变化吗?我回想起过去的问题,这些问题要求指针始终用NULL而不是0初始化,因为
一些实现中的NULL不一定是(void *)0。 (或者我认为的那些东西



不,C89具有相同的行为。


它仅适用于在编译时计算的零,而不适用于在运行时计算的零的
。例如在


char * p = 0;

int i = 5,j = i / 10;

char * q = j;


然后0表示p是编译时间因此被视为空指针

常量,但q表示0是运行时间不需要有相同的位

模式,无需比较相同。

-

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





James Brown写道On 08/14/06 16:43,:
< blockquote class =post_quotes>
全部,


阅读常见问题解答(问题5.2)后,我看到:


an 具有值0的积分常数表达式在指针上下文中,
在编译时转换为空指针。常见问题继续说:


char * p = 0;

if(p!= 0)


是有效的事情。


这是C99标准的新变化吗?



否;它是在原始的1989 ANSI标准中。


我回想起过去的问题

要求指针始终用NULL初始化而不是0,因为

一些实现中的NULL不一定是(void *)0。 (或者其他什么

沿着我认为的那些行)



Something,确实。 NULL主要用于文档;

它是写零的一种方式,并说嘿,代码阅读器,

不要误以为这是一个号码;它是一个空的

指针常量。一个在指针上下文中写入NULL只是

,因为在字符上下文中写''\ 0'';一个普通的0会做完b / b
完全相同的事情,但给

读者提供尽可能多的提示并不是坏事。


即使在今天,NULL也不一定是(void *)0;它也可以是
是一个普通的0.(它甚至可以像_builtin_null一样魔术,所以

,因为_builtin_null的行为与空指针完全相同

常数。)如果你回忆沿着这些界限的东西

的历史可以追溯到十五年左右,它可能不得不用预先做好

with pre - 标准版本的C,当指针被处理的方式存在一些分歧时



有点偏离主题,但我也相信这现在符合C ++

标准的工作原理?



comp.lang.c ++就在大厅对面,那个狂野的

派对正在进行。 (它一定是一个派对;那里的每个人

都会过载。)


如果有任何关于此更改的提示,我将不胜感激来了,什么标准

包含这种新行为等。



1989.所有ANSI和ISO C标准。


-
Er ********* @ sun.com


" James Brown" < no*@home.netwrites:


阅读常见问题(问题5.2)后,我看到:


an 具有值0的积分常数表达式在指针上下文中,
在编译时转换为空指针。常见问题继续说:


char * p = 0;

if(p!= 0)


是有效的事情。


这是C99标准的新变化吗?我记得过去的问题

要求指针始终用NULL而不是0初始化,因为

一些实现中的NULL不一定是(void *)0。 (或者其他什么

沿着我认为的那些方向)


有点偏离主题,但我也相信这现在与C ++如何一致>
标准也适用吗?


如果有任何关于何时发生这种变化的提示,那么什么标准

包含这种新行为等等。



NULL和0都是空指针常量。事实上,NULL可以简单地定义为0.

你可能正在考虑一个事实* br / *的表示* >
空指针不一定是全位零。


这个:


char * p = 0;


保证将p设置为空指针值。那个空指针

值可能会也可能不会表示为全位 - 零。


是的,它很混乱,正如需要所示对于

comp.lang.c FAQ的第5部分,< http://www.c-faq.com/(您应该阅读)。


-

Keith Thompson(The_Other_Keith) ks***@mib.org < http ://www.ghoti.net/~kst>

圣地亚哥超级计算机中心< *< http://users.sdsc.edu/~kst>

我们必须做点什么。这是事情。因此,我们必须这样做。


All,

After reading the FAQ (Q 5.2) I see that:

an "integral constant expression with the value 0" in a pointer context is
converted to a null pointer at compile time. The FAQ goes on to say that:

char *p = 0;
if(p != 0)

are valid things to do.

Is this a new change in the C99 standard? I recall issues in the past which
required that pointers always be intialized with NULL rather than 0, because
some under implementions NULL was not necessarily (void*)0. (or something
along those lines I believe)

A little off-topic, but I also believe this is now inline with how the C++
standard works as well?

Would appreciate any hints as to when this change came about, what standards
contain this new behaviour etc.

Many thanks,
James Brown

解决方案

In article <Oc********************@pipex.net>,
James Brown <no*@home.netwrote:

>After reading the FAQ (Q 5.2) I see that:

>an "integral constant expression with the value 0" in a pointer context is
converted to a null pointer at compile time.

>Is this a new change in the C99 standard? I recall issues in the past which
required that pointers always be intialized with NULL rather than 0, because
some under implementions NULL was not necessarily (void*)0. (or something
along those lines I believe)

No, C89 has the same behaviour.

It only applies to zeroes that are computed at compile time, not
to zeroes that are computed at run time. For example in

char *p = 0;
int i = 5, j = i / 10;
char *q = j;

then the 0 for p is compile time and thus treated as a null pointer
constant, but the 0 for q is run time and need not have the same bit
pattern and need not compare the same.
--
"No one has the right to destroy another person''s belief by
demanding empirical evidence." -- Ann Landers




James Brown wrote On 08/14/06 16:43,:

All,

After reading the FAQ (Q 5.2) I see that:

an "integral constant expression with the value 0" in a pointer context is
converted to a null pointer at compile time. The FAQ goes on to say that:

char *p = 0;
if(p != 0)

are valid things to do.

Is this a new change in the C99 standard?

No; it was in the original 1989 ANSI Standard.

I recall issues in the past which
required that pointers always be intialized with NULL rather than 0, because
some under implementions NULL was not necessarily (void*)0. (or something
along those lines I believe)

"Something," indeed. NULL is mostly for documentation;
it''s a way of writing a zero and saying "Hey, code reader,
don''t get fooled into thinking this is a number; it''s a null
pointer constant." One writes NULL in pointer contexts just
as one writes ''\0'' in character contexts; a plain 0 would do
exactly the same thing, but it is no bad thing to give the
reader as many hints as possible.

Even today, NULL is not necessarily (void*)0; it can also
be a plain 0. (It can even be "magic" like _builtin_null, so
long as _builtin_null behaves exactly like a null pointer
constant.) If your recollection of "something along those lines"
dates back more than fifteen years or so it may have had to do
with pre-Standard versions of C, when there was some divergence
in the ways pointers were handled.

A little off-topic, but I also believe this is now inline with how the C++
standard works as well?

comp.lang.c++ is across the hall, that room where the wild
party is going on. (It must be quite a party; everyone there
is getting over-loaded.)

Would appreciate any hints as to when this change came about, what standards
contain this new behaviour etc.

1989. All ANSI and ISO C Standards.

--
Er*********@sun.com


"James Brown" <no*@home.netwrites:

After reading the FAQ (Q 5.2) I see that:

an "integral constant expression with the value 0" in a pointer context is
converted to a null pointer at compile time. The FAQ goes on to say that:

char *p = 0;
if(p != 0)

are valid things to do.

Is this a new change in the C99 standard? I recall issues in the past which
required that pointers always be intialized with NULL rather than 0, because
some under implementions NULL was not necessarily (void*)0. (or something
along those lines I believe)

A little off-topic, but I also believe this is now inline with how the C++
standard works as well?

Would appreciate any hints as to when this change came about, what standards
contain this new behaviour etc.

Both NULL and 0 are null pointer constants. In fact, NULL can be
defined simply as 0.

You''re probably thinking of the fact that the *representation* of a
null pointer isn''t necessarily all-bits-zero.

This:

char *p = 0;

is guaranteed to set p to a null pointer value. That null pointer
value may or may not be represented as all-bits-zero.

Yes, it''s confusing, as demonstrated by the need for section 5 of the
comp.lang.c FAQ, <http://www.c-faq.com/(which you should read).

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.


这篇关于NULL指针初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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