C ++中的空指针 [英] Null pointer in C++

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

问题描述

在C ++中,我声明一个空指针为 int * p = 0 ,这意味着零是整数指针类型的一个特殊常量, p 指向地址 0x0
当然, 0x0 必须是一个特殊的地址,C ++在分配变量/数组时不会触及。

解决方案

C ++标准定义了整数常量0转换为空指针。 这并不意味着空指针指向地址0x0



当然,使空指针具有除<$ c $之外的表示形式,这意味着文本'0'在转换为指针时变为空指针。 c> 0x0 是相当复杂的,所以大多数编译器只是让 0x0 为空指针地址,并确保没有任何分配在零。 p>

请注意,使用此零转换被认为是不好的风格。使用 NULL (这是一个预处理器宏定义为 0 0L 或其他零整数常量),或者,如果您的编译器足够新,以支持它, nullptr


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? Of course for that 0x0 would have to be an special address to which C++ never touches during allocation of variables/arrays.

解决方案

The C++ standard defines that the integer constant 0 converts to a null pointer. This does not mean that null pointers point to address 0x0. It just means that the text '0' turns into a null pointer when converted to a pointer.

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.

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.

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

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