为什么用于空指针地址为零? [英] Why is address zero used for the null pointer?

查看:182
本文介绍了为什么用于空指针地址为零?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C(或C ++为此事),指针是特殊的,如果他们有值为零:我劝他们释放内存后置指针为零,因为这意味着释放的指针又没有危险;当我调用malloc返回零值的指针,如果它不能让我记忆;我使用如果(P!= 0)所有的时间,以确保传递指针是有效的,等等。

In C (or C++ for that matter), pointers are special if they have the value zero: I am adviced to set pointers to zero after freeing their memory, because it means freeing the pointer again isn't dangerous; when I call malloc it returns a pointer with the value zero if it can't get me memory; I use if (p != 0) all the time to make sure passed pointers are valid, etc.

不过,由于内存寻址0开始,不为0,就像其他任何一个有效的地址?如何能0用于处理空指针,如果是这样的话?为什么不是负数空呢?

But since memory addressing starts at 0, isn't 0 just as a valid address as any other? How can 0 be used for handling null pointers if that is the case? Why isn't a negative number null instead?

编辑:

一堆好答案。我会总结一下已经pssed作为我自己的心间$ P $点的答案前$ P $有人说,并希望社会各界能够纠正我,如果我误解了。

A bunch of good answers. I'll summarize what has been said in the answers expressed as my own mind interprets it and hope that the community will correct me if I misunderstand.


  • 就像一切都在规划否则它是一个抽象。只是一个常数,没有真正涉及到地址0的C ++ 0x通过添加关键字 nullptr 强调了这一。

它甚至不是一个地址的抽象,它是由C标准的编译器可以,只要把它翻译为其它数字指定的常数,因为它可以确保它永远不会等于一个真实的地址,等于其它空如果指针0是不使用该平台最大的价值。

It's not even an address abstraction, it's the constant specified by the C standard and the compiler can translate it to some other number as long as it makes sure it never equals a "real" address, and equals other null pointers if 0 is not the best value to use for the platform.

在情况下,它不是一个抽象的,这是在早期的情况下,地址0由系统使用和关闭限制程序员。

In case it's not an abstraction, which was the case in the early days, the address 0 is used by the system and off limits to the programmer.

我的负数的建议是有点野的头脑风暴,我承认。使用地址的符号的整数是有点浪费,如果它意味着,除了空指针(-1或其他)的价值空间被平分正整数,使有效的地址和那些只是浪费负数之间。

My negative number suggestion was a little wild brainstorming, I admit. Using a signed integer for addresses is a little wasteful if it means that apart from the null pointer (-1 or whatever) the value space is split evenly between positive integers that make valid addresses and negative numbers that are just wasted.

如果任何数量总是由一个数据类型重新presentable,这是0.(大概1也是。我觉得一个位整数这将是0或1,如果未签名,或只是签署如果位有符号,或者两个位整数这将是[-2,1]。不过,你可能只是去为0是零,1表示在内存中唯一可字节)。

If any number is always representable by a datatype, it's 0. (Probably 1 is too. I think of the one-bit integer which would be 0 or 1 if unsigned, or just the signed bit if signed, or the two bit integer which would be [-2, 1]. But then you could just go for 0 being null and 1 being the only accessible byte in memory.)

不过有一些东西是在我的脑海解决。堆栈 溢出问题的 指向一个特定的固定地址 的告诉我,即使0空指针抽象,其他指针值不一定。这使我张贴另一个堆栈 溢出问题的 我所能要访问的地址零

Still there is something that is unresolved in my mind. The Stack Overflow question Pointer to a specific fixed address tells me that even if 0 for null pointer is an abstraction, other pointer values aren't necessarily. This leads me to post another Stack Overflow question, Could I ever want to access the address zero?.

推荐答案

2点:


  • 仅在源$ C ​​$ c中的常数0为空指针 - 编译器实现可以使用它想要或者在运行code需求的任何值。一些平台有一个特殊的指针值就是这样实现可能为空指针使用无效。的C常见问题有一个问题,说真的,有什么实际的机器真正使用非零空指针,或不同的再presentations为指针不同类型的?,即指出,正在使用的C源空指针,而重新在运行时不同psented $ p $ 0这个属性几个平台。 C ++标准有一张纸条,上面明确规定转换整型常量前pression与零值总是产生一个空指针,但转换其他前pressions碰巧具有零值不必产生一个空指针

  • only the constant value 0 in the source code is the null pointer - the compiler implementation can use whatever value it wants or needs in the running code. Some platforms have a special pointer value that's 'invalid' that the implementation might use as the null pointer. The C FAQ has a question, "Seriously, have any actual machines really used nonzero null pointers, or different representations for pointers to different types?", that points out several platforms that used this property of 0 being the null pointer in C source while represented differently at runtime. The C++ standard has a note that makes clear that converting "an integral constant expression with value zero always yields a null pointer, but converting other expressions that happen to have value zero need not yield a null pointer".

负值可能只是由平台为地址为可用 - C标准索性就选择了一些用来表示一个空指针和被选择为零。我真的不知道,如果其他定点值进行了审议。

a negative value might be just as usable by the platform as an address - the C standard simply had to chose something to use to indicate a null pointer, and zero was chosen. I'm honestly not sure if other sentinel values were considered.

有关空指针的唯一要求是:

The only requirements for a null pointer are:


  • 它保证比较不等于一个指向一个实际的对象

  • 任何两个空指针将相当于(C ++提炼这使得该只需要为指针,坚持同一类型)

这篇关于为什么用于空指针地址为零?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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