为什么地址零用于空指针?

Why is address zero used for the null pointer?(为什么地址零用于空指针?)
本文介绍了为什么地址零用于空指针?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

在 C(或 C++)中,如果指针的值为零,则它们是特殊的:建议我在释放内存后将指针设置为零,因为这意味着再次释放指针并不危险;当我调用 malloc 时,如果它无法获得内存,它会返回一个值为 0 的指针;我一直使用 if (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?

一堆很好的答案.我会根据我自己的想法来总结回答中所说的内容,如果我误解了,希望社区能够纠正我.

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 标准指定的常量,编译器可以将其转换为其他数字,只要它确保它永远不等于真实"地址,并且等于其他 null如果 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.

如果任何数字始终可以由数据类型表示,则它为 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 点:

  • 只有源代码中的常量值 0 是空指针——编译器实现可以在运行代码中使用它想要或需要的任何值.某些平台有一个特殊的指针值,该值是无效的",实现可能会将其用作空指针.C FAQ有一个问题,"说真的,有没有真机真的使用过非零空指针,或者不同的表示用于指向不同类型的指针?",指出几个平台使用 0 属性作为 C 源代码中的空指针,但在运行时表示不同.C++ 标准有一条注释明确指出,转换具有零值的整数常量表达式总是会产生一个空指针,但转换其他恰好具有零值的表达式不需要产生空指针".

  • 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++ 对此进行了改进,以便只需要保存指向相同类型的指针)

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

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

What do compilers do with compile-time branching?(编译器如何处理编译时分支?)
Can I use if (pointer) instead of if (pointer != NULL)?(我可以使用 if (pointer) 而不是 if (pointer != NULL) 吗?)
Checking for NULL pointer in C/C++(在 C/C++ 中检查空指针)
Math-like chaining of the comparison operator - as in, quot;if ( (5lt;jlt;=1) )quot;(比较运算符的数学式链接-如“if((5<j<=1)))
Difference between quot;if constexpr()quot; Vs quot;if()quot;(“if constexpr()之间的区别与“if())
C++, variable declaration in #39;if#39; expression(C++,if 表达式中的变量声明)