简体   繁体   English

C ++中的空指针

[英]Null pointer in C++

When in C++ I declare a null pointer to be int* p=0 , does that mean the zero is some special constant of integer pointer type, or does it mean that p is pointing to address 0x0 ? 在C ++中,我声明一个空指针是int* p=0 ,这是否意味着零是整数指针类型的某个特殊常量,或者它是否意味着p指向地址0x0 Of course for that 0x0 would have to be an special address to which C++ never touches during allocation of variables/arrays. 当然, 0x0必须是C ++在分配变量/数组时从不接触的特殊地址。

The C++ standard defines that the integer constant 0 converts to a null pointer. C ++标准定义整数常量0转换为空指针。 This does not mean that null pointers point to address 0x0 . 这并不意味着空指针指向地址0x0 It just means that the text '0' turns into a null pointer when converted to a pointer. 它只是意味着当转换为指针时,文本“0”变为空指针。

Of course, making null pointers have a representation other than 0x0 is rather complicated, so most compilers just let 0x0 be the null pointer address and make sure nothing is ever allocated at zero. 当然,使空指针具有除0x0表示相当复杂,因此大多数编译器只是让0x0成为空指针地址并确保在零处分配任何内容。

Note that using this zero-conversion is considered bad style. 请注意,使用此零转换被视为不良样式。 Use NULL (which is a preprocessor macro defined as 0 , 0L , or some other zero integral constant), or, if your compiler is new enough to support it, nullptr . 使用NULL (其定义为一个预处理宏00L ,或其他一些零积分常数),或者,如果你的编译器足够新的支持的话, nullptr

It means that an integral constant expression with value zero has a special meaning in C++; 这意味着零值的整数常量表达式在C ++中具有特殊含义; it is called a null pointer constant . 它被称为空指针常量 when you use such an expression to initialize a pointer with, or to assign to a pointer, the implementation ensures that the pointer contains the appropriately typed null pointer value . 当您使用这样的表达式初始化指针或指定指针时,实现确保指针包含适当类型的空指针值 This is guaranteed to be a different value to any pointer pointing at a genuine object. 这保证与指向真实对象的任何指针的值不同。 It may or may not have a representation that is "zero". 它可能有也可能没有“零”的表示。

ISO/IEC 14882:2011 4.10 [conv.ptr] / 1: ISO / IEC 14882:2011 4.10 [conv.ptr] / 1:

A null pointer constant is an integral constant expression (5.19) prvalue of integer type that evaluates to zero or a prvalue of type std::nullptr_t . 空指针常量是整数类型的整数常量表达式(5.19)prvalue,其计算结果为零或类型为std::nullptr_t A null pointer constant can be converted to a pointer type; 空指针常量可以转换为指针类型; the result is the null pointer value of that type and is distinguishable from every other value of object pointer or function pointer type. 结果是该类型的空指针值 ,并且可以与对象指针或函数指针类型的每个其他值区分开。

It's a special value, which by the standard is guaranteed to never be equal to a pointer that is pointing to an object or a function. 它是一个特殊值,保证标准永远不会等于指向对象或函数的指针。 The address-of operator & will never yield the null pointer, nor will any successful dynamic memory allocations. address-of运算符&永远不会产生空指针,也不会产生任何成功的动态内存分配。 You should not think of it as address 0, but rather as special value that indicates that the pointer is pointing nowhere. 您不应将其视为地址0,而应将其视为指示指针无处指向的特殊值。 There is a macro NULL for this purpose, and the new idiom is nullptr . 为此目的有一个宏NULL ,新的习惯用法是nullptr

这意味着它没有指向任何东西。

The value of the pointer is just 0. It doesn't necessarily mean it points to address 0x0. 指针的值只是0.它并不一定意味着它指向地址0x0。 The NULL macro, is just a 0 constant. NULL宏,只是一个0常量。

指针指向地址0.在大多数非常特殊的平台上,你应该使用NULL ,因为它不总是0 (但很常见)。

Yes. 是。 Zero is a special constant. 零是一个特殊常数。 In fact, it's the only integral constant which can be used, without using explicit cast, in such statements: 实际上,它是唯一可以在这些语句中使用的整数常量,而不使用显式强制转换:

int *pi = 0;  //ok
char *pc = 0; //ok
void *pv = 0; //ok
A *pa = 0;    //ok

All would compile fine. 一切都会编译好。

However, if you use this instead: 但是,如果您使用此代码:

int *pi = 1;  //error
char *pc = 2; //error
void *pv = 3; //error
A *pa = 4;    //error

All would give compilation error. 所有都会给出编译错误。

In C++11, you should use nullptr , instead of 0 , when you mean null pointer. 在C ++ 11中,当你的意思是空指针时,你应该使用nullptr而不是0

In your example 'p' is the address of an int. 在您的示例中,'p'是int的地址。 By setting p to 0 you're saying there is an int at address 0. The convention is that 0 is the "not a valid address address", but its just a convention. 通过将p设置为0,你会说地址为0的int。 约定是0是“不是有效的地址”,但它只是一个约定。

In pratice address 0 is generally "unmapped" (that is there is no memory backing that address), so you will get a fault at that address. 在pratice中,地址0通常是“未映射”(即没有内存支持该地址),因此您将在该地址处出错。 That's not true in some embedded systems, though. 但是,在某些嵌入式系统中并非如此。

You could just as well pick any random address (eg 0xffff7777 or any other value) as the "null" address, but you would be bucking convention and confusing a lot of folks that read your code. 你也可以选择任何随机地址(例如0xffff7777或任何其他值)作为“空”地址,但你会违反常规并混淆许多阅读你代码的人。 Zero is generally used because most languages have support for testing is-zero is-not-zero efficiently. 通常使用零,因为大多数语言都支持有效测试零 - 非零。

See this related question: Why is address zero used for the null pointer? 请参阅此相关问题: 为什么地址零用于空指针?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM