NULL表示除了所有位0之外的表示 [英] NULL with representation other then all bits 0

查看:69
本文介绍了NULL表示除了所有位0之外的表示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!


有一个系统,其中0x0是有效地址,但0xffffffff不是。

如何处理空指针一个编译器(除了典型的

"解决方案仍然使用0x0表示null)?


- AFAIK C允许空指针以不同的方式表示

所有位0。这是正确的吗?

- AFAIK我不能#define NULL 0x10000''因为`void * p = 0;''应该工作

就像`void * p = NULL''。这是正确的吗?

- AFAIK我可以识别使用'0'作为指针的上下文,并使用数值0xffffffff而不是0x0来使用
。它是否正确?特别是

,应该`void * p;''将p初始化为空指针。而是

然后是零 (因此,如果没有表示空指针,则必须将其放在.data中,而不是.bss中,然后在bb中使用典型实现的


作为所有位0)?更糟糕的是,`memset(& p,0,sizeof(void *))''将p设置为

空指针而不是零?应该从int转换为void *

convert(int)0(bits:0x0)to(void *)0(bits:0xffffffff)?


I知道这个话题已经讨论了很多。这甚至是其中一个原因我不确定真正的答案是什么 - 我记得太多了

他们并不能告诉他们来自错误的那些......


提前致谢!

- Yossi

解决方案

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

嗨!

有一个系统,其中0x0是有效地址,但是0xffffffff不是。
编译器如何处理空指针(除了仍然使用0x0表示null的典型的解决方案)?


标准没有规定NULL指针的实际位表示,

是依赖于实现的。但是,标准确实规定

指针上下文中使用的0是NULL

指针的有效表示(即使它不是全部位 - 零)。

- AFAIK C允许空指针。以不同的方式表示然后
所有位0。它是否正确?


是(见上文)。

- AFAIK我不能#define NULL 0x10000''因为`void * p = 0;''应该工作
就像`void * p = NULL''。它是否正确?


是和否。是的,如果你是编译器编写器,并且在你的实现上

NULL指针表示是0x10000,所以你在

标准头文件中定义它。不,如果你想在你的
自己的C程序中重新定义NULL。在'since`之后你的句子中的部分与

这个上下文无关(但是真的)。​​

- AFAIK我可以识别使用0作为a的上下文指针和使用数值0xffffffff而不是0x0。它是否正确?在


是的,但你必须将其转换为适当的指针类型。但是,

这将使你的程序完全不可移植,因为你编译它的下一个

实现可能已将NULL定义为

完全不同(按位)。
特别是
,应该`void * p;''将p初始化为空指针。而是
然后零 (因此,如果空指针未表示为所有位0),则必须将其放置在.data而不是.bss的典型实现方面。更糟糕的是,应该'memset(& p,0,sizeof(void *)'''将p设置为空指针。而不是零?应该从int转换为void *
转换(int)0(位:0x0)到(void *)0(位:0xffffffff)?

我知道这个话题已经讨论过了许多。这甚至是其中一个原因我不确定真正的答案是什么 - 我记得太多了
其中许多并且不能从错误的那些中说出正确的答案。 。




我建议你仔细阅读 http://www.c-faq.com/

特别是第5节,它完全致力于NULL指针,

我相信你回答所有问题(直接链接是:
http://www.c-faq.com/null/index.html


干杯


弗拉基米尔

-

单身汉是一个自私的,不值得的家伙,已经骗了一些女人离婚。$ / b
- Don Quinn


Vladimir S. Oka写道:

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


<关于空指针表示的东西不是所有位0>

- AFAIK我可以识别使用0作为指针的上下文,并使用数值0xffffffff而不是0x0。它是否正确?在



是的,但你必须将其转换为适当的指针类型。但是,这将使你的程序完全不可移植,因为你编译它的下一个实现可能已经定义了NULL作为完全不同的东西(逐位)。




< snip>


实际上,即使这是空指针的正确表示

转换可能定义为转换0xffffffff实际上

转换为地址0,否则你将无法生成

地址0.

- -

Flash Gordon

生活在有趣的时代。

虽然我的电子邮件地址说垃圾邮件,但它是真的,我读了它。


>有一个系统,其中0x0是有效地址,但0xffffffff不是。

编译器如何处理空指针(除了仍然使用0x0作为null的典型的解决方案之外? - AFAIK C允许空指针。以不同的方式表示然后
所有位0。它是否正确?


是的。

- AFAIK我不能#define NULL 0x10000''因为`void * p = 0;''应该工作
就像`void * p = NULL''。它是否正确?


作为程序员,你不允许这样做。

作为编译器实现者,你可以这样做。


没有禁止实现,其中具有

a主值位模式的所有指针都被视为空指针。这可能

使得为指针比较生成的代码变得凌乱,因为两个null

指针应该相互比较,即使它们没有

相同的位表示。

- AFAIK我可以识别使用0作为指针的上下文并使用数值0xffffffff而不是0x0。它是否正确?在


你可以识别空指针常量并使用正确的位

模式(在32位机器上为0xdeadbeef,而不是0xffffffff)

在编译时。

特别是,应该`void * p;''将p初始化为null指针而是
然后零 (因此,如果空指针未表示为所有位0),则必须将其放置在.data而不是.bss的典型实现方面。


你可以把它放在一个初始化为空指针的部分,

不是所有位 - 零。

更糟,应该`memset(& p,0,sizeof(void *))''将p设置为空指针。而不是零?


No.

memset(& p,0,sizeof(void *));

p;这里允许使用/ *包裹错误* /

应该从int转换为void *
转换(int)0(位:0x0)到(void *)0(位:0xffffffff)?


来自非常量整数的投射不需要进行这样的转换。

我知道这个话题已经讨论了很多。这甚至是其中一个原因我不确定真正的答案是什么 - 我记得他们太多了,不能从错误的那些人那里说出正确的答案。 。




Gordon L. Burditt


Hi!

There is a system where 0x0 is a valid address, but 0xffffffff isn''t.
How can null pointers be treated by a compiler (besides the typical
"solution" of still using 0x0 for "null")?

- AFAIK C allows "null pointers" to be represented differently then
"all bits 0". Is this correct?
- AFAIK I can''t `#define NULL 0x10000'' since `void* p=0;'' should work
just like `void* p=NULL''. Is this correct?
- AFAIK I can identify contexts where `0'' is used as a pointer and use
the numeric value 0xffffffff rather then 0x0. Is this correct? In
particular, should `void* p;'' initialize p to "null pointer" rather
then "zero" (so it has to be placed in ".data" rather then ".bss" in
terms of typical implementations if "null pointer" is not represented
as all bits 0)? Worse, should `memset(&p, 0, sizeof(void*))'' set p to
the "null pointer" rather then "zero"? Should casts from int to void*
convert (int)0 (bits: 0x0) to (void*)0 (bits: 0xffffffff)?

I know that this topic has been discussed a lot. That''s even one of the
reasons I''m not sure what the real answers are - I remember too many of
them and can''t tell the right ones from the wrong ones...

Thanks in advance!
-- Yossi

解决方案

yo***********@gmail.com wrote:

Hi!

There is a system where 0x0 is a valid address, but 0xffffffff isn''t.
How can null pointers be treated by a compiler (besides the typical
"solution" of still using 0x0 for "null")?
Standard does not prescribe actual bit representation of a NULL pointer,
that is implementation dependent. However, the Standard does prescribe
that 0 used in a pointer context is a valid representation of a NULL
pointer (even if it''s not all-bits-zero).

- AFAIK C allows "null pointers" to be represented differently then
"all bits 0". Is this correct?
Yes (see above).
- AFAIK I can''t `#define NULL 0x10000'' since `void* p=0;'' should work
just like `void* p=NULL''. Is this correct?
Yes and no. Yes, if you''re compiler writer, and on your implementation
NULL pointer representation is 0x10000, so you defined it thus in the
standard header files. No, if you''re trying to re-define NULL in your
own C program. The part of your sentence after `since` is irrelevant in
this context (but true).
- AFAIK I can identify contexts where `0'' is used as a pointer and use
the numeric value 0xffffffff rather then 0x0. Is this correct? In
Yes, but you''d have to cast it an appropriate pointer type. However,
this will make your program utterly non-portable, as the next
implementation you compile it on may have defined NULL as something
entirely different (bit-wise).
particular, should `void* p;'' initialize p to "null pointer" rather
then "zero" (so it has to be placed in ".data" rather then ".bss" in
terms of typical implementations if "null pointer" is not represented
as all bits 0)? Worse, should `memset(&p, 0, sizeof(void*))'' set p to
the "null pointer" rather then "zero"? Should casts from int to void*
convert (int)0 (bits: 0x0) to (void*)0 (bits: 0xffffffff)?

I know that this topic has been discussed a lot. That''s even one of
the reasons I''m not sure what the real answers are - I remember too
many of them and can''t tell the right ones from the wrong ones...



I suggest you carefully read the C FAQ at http://www.c-faq.com/,
especially Section 5 which is in its entirety devoted to NULL pointers,
and I believe answers all your questions (direct link is:
http://www.c-faq.com/null/index.html)

Cheers

Vladimir
--
A bachelor is a selfish, undeserving guy who has cheated some woman out
of a divorce.
-- Don Quinn


Vladimir S. Oka wrote:

yo***********@gmail.com wrote:



<snip stuff about null pointer representation being not all bits 0>

- AFAIK I can identify contexts where `0'' is used as a pointer and use
the numeric value 0xffffffff rather then 0x0. Is this correct? In



Yes, but you''d have to cast it an appropriate pointer type. However,
this will make your program utterly non-portable, as the next
implementation you compile it on may have defined NULL as something
entirely different (bit-wise).



<snip>

Actually, even if that is the correct representation of a null pointer
the conversion might defined so that converting 0xffffffff actually
converts to address 0 since otherwise you would not be able to produce
an address 0.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.


>There is a system where 0x0 is a valid address, but 0xffffffff isn''t.

How can null pointers be treated by a compiler (besides the typical
"solution" of still using 0x0 for "null")?

- AFAIK C allows "null pointers" to be represented differently then
"all bits 0". Is this correct?
Yes.
- AFAIK I can''t `#define NULL 0x10000'' since `void* p=0;'' should work
just like `void* p=NULL''. Is this correct?
You, as programmer, are not allowed to do this.
You, as compiler implementor, are allowed to do this.

There is no prohibition of an implementation where "all pointers with
a prime-valued bit pattern are considered null pointers. This might
make the code generated for pointer comparison messy, since two null
pointers should compare equal to each other even if they don''t have
the same bit representation.
- AFAIK I can identify contexts where `0'' is used as a pointer and use
the numeric value 0xffffffff rather then 0x0. Is this correct? In
You can identify null pointer constants and use the correct bit
pattern (which is 0xdeadbeef, not 0xffffffff on 32-bit machines)
at compile time.
particular, should `void* p;'' initialize p to "null pointer" rather
then "zero" (so it has to be placed in ".data" rather then ".bss" in
terms of typical implementations if "null pointer" is not represented
as all bits 0)?
You could place it in a section which is initialized to null pointers,
not all-bits-zero.
Worse, should `memset(&p, 0, sizeof(void*))'' set p to
the "null pointer" rather then "zero"?
No.
memset(&p, 0, sizeof(void*));
p; /* smegmentation fault allowed here */
Should casts from int to void*
convert (int)0 (bits: 0x0) to (void*)0 (bits: 0xffffffff)?
Casts from non-constant ints need not do such a conversion.
I know that this topic has been discussed a lot. That''s even one of the
reasons I''m not sure what the real answers are - I remember too many of
them and can''t tell the right ones from the wrong ones...



Gordon L. Burditt


这篇关于NULL表示除了所有位0之外的表示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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