C++ 什么是空指针,什么是空指针?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4334831/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 15:09:00  来源:igfitidea点击:

What is a void pointer and what is a null pointer?

c++pointersvoid-pointersnull-pointer

提问by Shouvik

So I was going through some interview questions and I came across one about void and null pointers, which claims:

所以我正在经历一些面试问题,我遇到了一个关于 void 和 null pointers 的问题,它声称:

a pointer with no return type is called a null pointer. It may be any kind of datatype.

没有返回类型的指针称为空指针。它可以是任何类型的数据类型。

This confused me thoroughly! It seems void and null could be used interchangeably according to this question, and I don't believe that to be correct. I assumed void to be a return type and null to be a value. But I am just a code-rookie and am not sure I am right.

这让我彻底糊涂了!根据这个问题,似乎 void 和 null 可以互换使用,我认为这是不正确的。我假设 void 是一个返回类型,null 是一个值。但我只是一个代码菜鸟,不确定我是对的。

Please express your views as to what a null pointer is and a void pointer is. I am not looking for difference between null and void.

请表达您对什么是空指针和空指针的看法。我不是在寻找无效和无效之间的区别。

回答by Marcelo Cantos

The two concepts are orthogonal:

这两个概念是正交的:

  1. A void pointer, (void *) is a raw pointer to some memory location.
  2. A null pointer is a special pointer that doesn't point to anything, by definition. It can be a pointer to any type, void or otherwise.
  1. void 指针 ( void *) 是指向某个内存位置的原始指针。
  2. 根据定义,空指针是一个特殊的指针,它不指向任何东西。它可以是指向任何类型、void 或其他类型的指针。

A void pointer can be null or not:

空指针可以为空或不为空:

void *void_ptr1 = nullptr;
void *void_ptr2 = malloc(42);
void *void_ptr3 = new Foo;               // void * can point to almost anything
void *void_ptr4 = (char*)void_ptr3 + 1;  // even somewhere inside an object

A non-void pointer can also be null or not:

非空指针也可以为空或不为空:

Foo *f = nullptr;
Foo *g = new Foo;

回答by Armen Tsirunyan

Just plain forget about that answer. A quote from your link :

只是简单地忘记那个答案。来自您的链接的报价:

"a pointer with no return type is called a null pointer."

“没有返回类型的指针称为空指针。”

This is sooo plain WRONG. A pointer's return type? REALLY? This is a bad source...

这是非常错误的。指针的返回类型? 真的吗?这是一个糟糕的来源...

void*is universal pointer typebecause any pointer type (except for pointer to const and/or volatile) can be implicitly converted to void*. In other words, you can assign any pointer to a variable of type void*. A null pointer is a pointer value0

void*是通用指针类型,因为任何指针类型(指向 const 和/或 volatile 的指针除外)都可以隐式转换为void*. 换句话说,您可以将任何指针分配给类型为 的变量void*。空指针是指针0

回答by Matteo Italia

The voidtype in general means that no type information is given.

void没有任何类型的信息赋予一般手段类型。

You should always keep in mind that a pointer conveys two pieces of information: the typeof the pointed data (int, double, ...), which specifies how to interpret it, and the addressof the data it points to, which specifies whereyou can get the actual value of the pointed data.

您应该始终牢记,指针传达了两条信息:指向数据的类型( int, double, ...),它指定如何解释它,以及它指向的数据的地址,它指定您在哪里可以得到指向数据的实际值。

The type information is in the type of the pointer (double*, int*, ...), while the address of the data is the actual value contained in the pointer variable.

类型信息在指针的类型中(, , ...),而数据的地址是指针变量中包含的实际值。double*int*

So, a voidpointer (void *) is a pointer that do not specify any type information. It tells you where the data is, but it doesn't tell you how to interpret it. You know that at that address there's something, but you don't know if it's an int, a doubleor an array of flying cows. To actually use such data, you have to get type information about it in some other way (e.g. with some other magic parameter), cast that pointer to a regular pointer type and then use it as usual.

因此,void指针 ( void *) 是不指定任何类型信息的指针。它会告诉您数据的位置,但不会告诉您如何解释数据。你知道在那个地址有东西,但你不知道它是int一头、一头double还是一群飞牛。要实际使用此类数据,您必须以其他方式(例如,使用其他魔术参数)获取有关它的类型信息,将该指针转换为常规指针类型,然后照常使用它。

void *is often used in C to provide some kind of support to generic programming; see for example the qsortC library function.

void *在 C 中经常用于为泛型编程提供某种支持;参见例如qsortC 库函数。

A NULLpointer, instead, is a pointer that points to nothing. In this case, the type information about the pointer in general is present, but it's the address of the pointed data that is missing. Of course, it's possible to have a void *that is NULL.

一个NULL指针,取而代之的,是一个指针,它指向什么。在这种情况下,通常存在有关指针的类型信息,但缺少的是指向数据的地址。当然,有可能void *NULL.

Quick example (supposing that vis declared as double v;):

快速示例(假设v声明为double v;):

                         Type information present
             +----------------------+----------------------+
             |          ?           |          ?           |
         +---+----------------------+----------------------+
    p  c |   |                      |                      |
 v  o  o | ? | double * ptr = &v;   | void * ptr = &v;     |
 a  i  n |   |                      |                      |
 l  n  t +---+----------------------+----------------------+
 i  t  e |   |                      |                      |
 d  e  n | ? | double * ptr = NULL; | void * ptr = NULL;   |
    d  t |   |                      |                      |
         +---+----------------------+----------------------+

Trivia: NULL, at least in the current standard, is guaranteed to be 0.

琐事NULL至少在当前标准中,保证为 0。

In other areas of the language, voidis always used to specify lack of type. Using it as return value (note: I'm talking now about void, not void *) means that the function does not return any value, and casting an expression to void is a fancy way to discard a value (you're signaling to the compiler and to other programmers that you're conscious that you're not using a certain value).

在语言的其他领域,void总是用来指​​定缺少类型。使用它作为返回值(注意:我说的是现在约void,不void *)表示函数不返回任何值,并投了表达空间被一个奇特的方式丢弃的值(你是信令编译器和其他程序员,你意识到你没有使用某个值)。

回答by Daniel Mo?mondor

Please, tell us: whats the difference:

请告诉我们:有什么区别:

  • between gas tank and no-gas situation
  • between cookie jar and no-cookies
  • between term 'money' and 'empty pockets'
  • 在油箱和无气情况之间
  • 饼干罐和无饼干之间
  • 在术语“钱”和“空口袋”之间

If you come up with these, you'l be able to grasp null vs void* dillema.

如果你想出这些,你就能够理解 null vs void* 困境。

回答by Nicolas Repiquet

voidis a non-type. nullis a non-value.

void是非类型。 null是一个非值。

回答by Chubsdad

Here's some differences with respect to pointer arithmetic:

以下是关于指针运算的一些差异:

It stems from the fact that void is an incomplete type.

它源于这样一个事实,即 void 是一种不完整的类型。

void *vp;
vp++;     // error, incomplete type
vp += 2;  // same error

void *p = 0;
p++;      // still same error

int *p = 0;
p++;      // well-formed program, but UB (.6/5)

回答by unwind

The linked article is simply wrong. Its first sentence:

链接的文章是完全错误的。它的第一句话:

a pointer with no return type is called a null pointer

没有返回类型的指针称为空指针

is triggering all sorts of alarms for me. This is a highly confused piece of writing.

正在为我触发各种警报。这是一篇非常混乱的文章。

You are almost correct. "Pointer to void" is a type (not a "return type"). Values of any type can be returned by functions, and thus be (the function's) return type.

你几乎是正确的。“指向 void 的指针”是一种类型(不是“返回类型”)。任何类型的值都可以由函数返回,因此是(函数的)返回类型。

A null pointer is a pointer that, regardless of its type, is pointing at the null object, which is not any valid object that can be created. A null pointer can be said to point at "nothing".

空指针是一个指针,无论其类型如何,都指向空对象,它不是任何可以创建的有效对象。可以说空指针指向“无”。

A pointer to void can also be null;

指向 void 的指针也可以为 null;

void *nothing = 0;

is perfectly valid code, and just says that this pointer is capable of pointing a an untyped object, but right now it isn't.

是完全有效的代码,只是说这个指针能够指向一个无类型的对象,但现在它不是。

回答by Vladimir Ivanov

null pointer is point to 0x000000(which is incorrect to access pointer), while void pointer is a correct pointer to an unspecified type(void *). However, void pointer can be null pointer, but then unreferencing the pointer will generate error.

空指针指向 0x000000(访问指针不正确),而空指针是指向未指定类型(void *)的正确指针。但是,void 指针可以是空指针,但是取消引用该指针会产生错误。

回答by anurag tiwari

A void *ptris the pointer which can be used to point any type of data. It maybe int, float, double. It has no return type that is initially pointer is created with pointer type (having hex value) and we can assign this pointer to any type of data.

Avoid *ptr是可用于指向任何类型数据的指针。它也许intfloatdouble。它没有返回类型,初始指针是用指针类型(具有十六进制值)创建的,我们可以将此指针分配给任何类型的数据。

While null pointer is the pointer with having NULL value as address, the pointer is assigned NULL value so that it cannot be used to access others data which its address may contain while creation. I think it is good programming technique to assign a pointer NULL if its not used at the moment.

虽然空指针是以NULL值作为地址的指针,但该指针被分配了NULL值,因此它不能用于访问创建时其地址可能包含的其他数据。我认为如果当前不使用指针 NULL,则将其分配为 NULL 是一种很好的编程技术。