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

时间:2021-06-25 13:25:12

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

我在面试中遇到了一些问题我遇到了一个关于void和空指针的问题

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.

这彻底把我都弄糊涂了!根据这个问题,它似乎是无效的,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.

请表示您对空指针和空指针的看法。我不是在寻找null和void之间的区别。

9 个解决方案

#1


38  

The two concepts are orthogonal:

这两个概念是正交的:

  1. A void pointer, (void *) is a raw pointer to some memory location.
  2. 空指针(void *)是指向某个内存位置的原始指针。
  3. 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.
  4. 空指针是一种特殊的指针,根据定义,它不指向任何东西。它可以是指向任何类型的指针,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;

#2


27  

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 type because 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 value 0

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

#3


12  

The void type 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 type of the pointed data (int, double, ...), which specifies how to interpret it, and the address of the data it points to, which specifies where you can get the actual value of the pointed data.

你应该记住,一个指针传达了两个信息:指出数据的类型(int,双,…),它指定如何解释它,和它指向的地址数据,它指定的地方你可以指出的实际价值的数据。

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 void pointer (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 double or 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 qsort C library function.

在C中经常使用void *来为通用编程提供某种支持;参见qsort C库函数。

A NULL pointer, 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.

而空指针则是指向无的指针。在本例中,通常存在关于指针的类型信息,但丢失的是指向数据的地址。当然,可能有一个空*是空的。

Quick example (supposing that v is 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, void is 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 *)意味着函数不返回任何值,和铸造一个表达式无效是一个奇特的方式丢弃一个值(你信号编译器和其他程序员,你意识到你没有使用一个特定的值)。

#4


9  

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.

如果你想到了这些,你就能理解零vs虚空*迪勒马。

#5


7  

void is a non-type. null is a non-value.

空虚是实。零是一个无值。

#6


2  

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 ($5.6/5)

#7


1  

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.

是完全有效的代码,并且只说这个指针可以指向一个非类型化的对象,但是现在它不是。

#8


1  

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指针可以是空指针,但是不引用指针会产生错误。

#9


1  

A void *ptr is 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.

void *ptr是一个指针,可以用来指向任何类型的数据。可能是int, float, double。它没有返回类型,最初指针是用指针类型(具有十六进制值)创建的,我们可以将这个指针分配给任何类型的数据。

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是一种很好的编程技术。

#1


38  

The two concepts are orthogonal:

这两个概念是正交的:

  1. A void pointer, (void *) is a raw pointer to some memory location.
  2. 空指针(void *)是指向某个内存位置的原始指针。
  3. 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.
  4. 空指针是一种特殊的指针,根据定义,它不指向任何东西。它可以是指向任何类型的指针,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;

#2


27  

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 type because 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 value 0

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

#3


12  

The void type 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 type of the pointed data (int, double, ...), which specifies how to interpret it, and the address of the data it points to, which specifies where you can get the actual value of the pointed data.

你应该记住,一个指针传达了两个信息:指出数据的类型(int,双,…),它指定如何解释它,和它指向的地址数据,它指定的地方你可以指出的实际价值的数据。

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 void pointer (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 double or 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 qsort C library function.

在C中经常使用void *来为通用编程提供某种支持;参见qsort C库函数。

A NULL pointer, 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.

而空指针则是指向无的指针。在本例中,通常存在关于指针的类型信息,但丢失的是指向数据的地址。当然,可能有一个空*是空的。

Quick example (supposing that v is 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, void is 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 *)意味着函数不返回任何值,和铸造一个表达式无效是一个奇特的方式丢弃一个值(你信号编译器和其他程序员,你意识到你没有使用一个特定的值)。

#4


9  

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.

如果你想到了这些,你就能理解零vs虚空*迪勒马。

#5


7  

void is a non-type. null is a non-value.

空虚是实。零是一个无值。

#6


2  

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 ($5.6/5)

#7


1  

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.

是完全有效的代码,并且只说这个指针可以指向一个非类型化的对象,但是现在它不是。

#8


1  

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指针可以是空指针,但是不引用指针会产生错误。

#9


1  

A void *ptr is 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.

void *ptr是一个指针,可以用来指向任何类型的数据。可能是int, float, double。它没有返回类型,最初指针是用指针类型(具有十六进制值)创建的,我们可以将这个指针分配给任何类型的数据。

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是一种很好的编程技术。