空指针和空指针有什么区别?

时间:2021-02-12 13:29:54

Whats the difference between a Null pointer & a Void pointer?

空指针和空指针的区别是什么?

10 个解决方案

#1


92  

Null pointer is a special reserved value of a pointer. A pointer of any type has such a reserved value. Formally, each specific pointer type (int *, char * etc.) has its own dedicated null-pointer value. Conceptually, when a pointer has that null value it is not pointing anywhere.

空指针是指针的特殊保留值。任何类型的指针都具有这样的保留值。形式上,每个特定的指针类型(int *, char *等)都有自己专用的空指针值。从概念上讲,当指针具有空值时,它不会指向任何地方。

Void pointer is a specific pointer type - void * - a pointer that points to some data location in storage, which doesn't have any specific type.

Void指针是一个特定的指针类型- Void * -一个指向存储中某些数据位置的指针,它没有任何特定类型。

So, once again, null pointer is a value, while void pointer is a type. These concepts are totally different and non-comparable. That essentially means that your question, as stated, is not exactly valid. It is like asking, for example, "What is the difference between a triangle and a car?".

所以,再一次,空指针是一个值,而void指针是一个类型。这些概念是完全不同的和不可比拟的。这实际上意味着你的问题,如前所述,并不完全有效。就像问,例如,“三角形和汽车有什么区别?”

#2


9  

They are two different concepts: "void pointer" is a type (void *). "null pointer" is a pointer that has a value of zero (NULL). Example:

它们是两个不同的概念:“空指针”是一个类型(void *)。“空指针”是一个值为零(null)的指针。例子:

void *pointer = NULL;

That's a NULL void pointer.

这是一个空空指针。

#3


4  

A null pointer is guaranteed to not compare equal to a pointer to any object. It's actual value is system dependent and may vary depending on the type. To get a null int pointer you would do

空指针被保证不与任何对象的指针相比。它的实际值是依赖于系统的,并且可能根据类型而变化。得到一个空int指针。

int* p = 0;

A null pointer will be returned by malloc on failure.

一个空指针将被malloc以失败的形式返回。

We can test if a pointer is null, i.e. if malloc or some other function failed simply by testing its boolean value:

我们可以测试一个指针是否为空,即如果malloc或其他函数通过测试其布尔值而失败:

if (p) {
    /* Pointer is not null */
} else {
    /* Pointer is null */
}

A void pointer can point to any type and it is up to you to handle how much memory the referenced objects consume for the purpose of dereferencing and pointer arithmetic.

空指针可以指向任何类型,它由您来处理引用的对象消耗多少内存,以便取消引用和指针算法。

#4


2  

Void refers to the type. Basically the type of data that it points to is unknown.

Void是指类型。基本上它指向的数据类型是未知的。

Null refers to the value. It's essentially a pointer to nothing, and is invalid to use.

Null是指值。它本质上是一个指向nothing的指针,并且使用起来是无效的。

#5


2  

A null pointer points has the value NULL which is typically 0, but in any case a memory location which is invalid to dereference. A void pointer points at data of type void. The word "void" is not an indication that the data referenced by the pointer is invalid or that the pointer has been nullified.

空指针点的值为null,通常为0,但在任何情况下都是无效的内存位置。空指针指向void类型的数据。“void”一词并不是指指针引用的数据无效或指针已被取消。

#6


1  

Usually a null pointer (which can be of any type, including a void pointer !) points to:

通常一个空指针(可以是任何类型,包括空指针!)指向:

  • the address 0, against which most CPU instructions sets can do a very fast compare-and-branch (to check for uninitialized or invalid pointers, for instance) with optimal code size/performance for the ISA.

    地址0,大多数CPU指令集可以做一个非常快的比较和分支(例如检查未初始化的或无效的指针),并为ISA提供最优的代码大小/性能。

  • an address that's illegal for user code to access (such as 0x00000000 in many cases), so that if a code actually tries to access data at or near this address, the OS or debugger can easily stop or trap a program with this bug.

    对于用户代码访问(在许多情况下是0x00000000)的地址是非法的,因此如果一个代码实际上试图访问这个地址或附近的数据,操作系统或调试器可以很容易地停止或用这个bug捕获一个程序。

A void pointer is usually a method of cheating or turning-off compiler type checking, for instance if you want to return a pointer to one type, or an unknown type, to use as another type. For instance malloc() returns a void pointer to a type-less chunk of memory, the type of which you can cast to later use as a pointer to bytes, short ints, double floats, typePotato's, or whatever.

空指针通常是一种欺骗或关闭编译器类型检查的方法,例如,如果您想返回一个类型的指针,或者一个未知类型,作为另一种类型。例如,malloc()将一个空指针返回到一个类型较少的内存块中,您可以将其转换为一个指向字节、短int、double float、typePotato或其他的指针。

#7


1  

NULL is a value that is valid for any pointer type. It represents the absence of a value.

NULL是一个对任何指针类型都有效的值。它表示没有值。

A void pointer is a type. Any pointer type is convertible to a void pointer hence it can point to any value. This makes it good for general storage but bad for use. By itself it cannot be used to access a value. The program must have extra context to understand the type of value the void pointer refers to before it can access the value.

空指针是一种类型。任何指针类型都可以转换为空指针,因此它可以指向任何值。这使它对一般存储有利,但对使用不利。它本身不能用于访问一个值。程序必须有额外的上下文来理解空指针所指的值类型,然后才能访问该值。

#8


1  

Null pointers and void pointers are completely different from each other. If we request the operating system(through malloc() in c langauge) to allocate memory for a particular data type then the operating system allocates memory in heap (if space is available in heap) and sends the address of the memory which was allocated.

空指针和空指针完全不同。如果我们请求操作系统(通过malloc()在c语言中)为特定的数据类型分配内存,那么操作系统将在堆中分配内存(如果在堆中可用空间),并发送分配给内存的地址。

When memory is allocated by os in heap then we can assign this address value in any pointer type variable of that data type. This pointer is then called a void pointer until it is not taken for any process.

当内存由os在堆中分配时,我们可以将该地址值分配到该数据类型的任何指针类型变量中。然后这个指针被称为空指针,直到它没有被执行任何进程。

When the space is not available in heap then the operating system certainly allocates memory and sends an address value of that location but this memory is not allocated in heap by the os because there is no space in heap,in this case this memory is allocated by the os in the system memory.. This memory can not be accessed by the user hence when we assign this address value in a pointer then this pointer is known as null pointer, and we cannot use this pointer. In the case of void pointer we can use it for any process in any programming language.

当堆中可用的空间不那么肯定操作系统分配内存并发送一个地址值的位置,但这不是堆中分配的内存堆中的操作系统,因为没有空间,在这种情况下,这个操作系统的内存分配系统内存. .这个内存不能被用户访问,因此当我们在一个指针中分配这个地址值时,这个指针被称为空指针,我们不能使用这个指针。在void指针的情况下,我们可以在任何编程语言中使用它。

#9


0  

I don't think AnT's answer is correct.

我认为蚂蚁的回答不正确。

  1. NULL is just a pointer constant, otherwise how could we have ptr = NULL.
  2. NULL是一个指针常量,否则我们怎么能有ptr = NULL。
  3. As NULL is a pointer, what's its type. I think the type is just (void *), otherwise how could we have both int * ptr = NULL and (user-defined type)* ptr = NULL. void type is actually a universal type.
  4. NULL是指针,它的类型是什么。我认为类型只是(void *),否则我们如何同时拥有int * ptr = NULL和(用户定义类型)* ptr = NULL。void类型实际上是一个通用类型。
  5. Quoted in "C11(ISO/IEC 9899:201x) §6.3.2.3 Pointers Section 3":

    An integer constant expression with the value 0, or such an expression cast to type void *, is called a null pointer constant

    值为0的整数常量表达式,或将表达式转换为void *,称为空指针常量。

So simply put: NULL pointer is a void pointer constant.

简单地说,空指针是一个空指针常量。

#10


0  

Null pointer is a pointer which is not pointing to anything, it is used in cases where a pointer is not assigned a valid address in the memory. Every pointer type i.e int *, char * each have a null pointer value of its own.

空指针是一个指针,它没有指向任何东西,它用于在内存中没有指定一个有效地址的指针。我每一个指针类型。e int *, char *每个都有它自己的空指针值。

A void pointer is one which doesn't have any data type associated with it, i.e it can be assigned value of any type. Also known as the general purpose pointer, it is a C convention for a raw address.

空指针是一个没有任何与之相关的数据类型的指针。它可以被赋值任何类型。也称为通用指针,它是一个原始地址的C约定。

So, a null pointer is basically a null value assigned to a pointer of any data type whereas a void pointer is a data type which remains void as long as an address of a data type is not assigned to it.

因此,空指针基本上是赋给任何数据类型指针的空值,而空指针是一种数据类型,只要不分配数据类型的地址,它就仍然是空的。

#1


92  

Null pointer is a special reserved value of a pointer. A pointer of any type has such a reserved value. Formally, each specific pointer type (int *, char * etc.) has its own dedicated null-pointer value. Conceptually, when a pointer has that null value it is not pointing anywhere.

空指针是指针的特殊保留值。任何类型的指针都具有这样的保留值。形式上,每个特定的指针类型(int *, char *等)都有自己专用的空指针值。从概念上讲,当指针具有空值时,它不会指向任何地方。

Void pointer is a specific pointer type - void * - a pointer that points to some data location in storage, which doesn't have any specific type.

Void指针是一个特定的指针类型- Void * -一个指向存储中某些数据位置的指针,它没有任何特定类型。

So, once again, null pointer is a value, while void pointer is a type. These concepts are totally different and non-comparable. That essentially means that your question, as stated, is not exactly valid. It is like asking, for example, "What is the difference between a triangle and a car?".

所以,再一次,空指针是一个值,而void指针是一个类型。这些概念是完全不同的和不可比拟的。这实际上意味着你的问题,如前所述,并不完全有效。就像问,例如,“三角形和汽车有什么区别?”

#2


9  

They are two different concepts: "void pointer" is a type (void *). "null pointer" is a pointer that has a value of zero (NULL). Example:

它们是两个不同的概念:“空指针”是一个类型(void *)。“空指针”是一个值为零(null)的指针。例子:

void *pointer = NULL;

That's a NULL void pointer.

这是一个空空指针。

#3


4  

A null pointer is guaranteed to not compare equal to a pointer to any object. It's actual value is system dependent and may vary depending on the type. To get a null int pointer you would do

空指针被保证不与任何对象的指针相比。它的实际值是依赖于系统的,并且可能根据类型而变化。得到一个空int指针。

int* p = 0;

A null pointer will be returned by malloc on failure.

一个空指针将被malloc以失败的形式返回。

We can test if a pointer is null, i.e. if malloc or some other function failed simply by testing its boolean value:

我们可以测试一个指针是否为空,即如果malloc或其他函数通过测试其布尔值而失败:

if (p) {
    /* Pointer is not null */
} else {
    /* Pointer is null */
}

A void pointer can point to any type and it is up to you to handle how much memory the referenced objects consume for the purpose of dereferencing and pointer arithmetic.

空指针可以指向任何类型,它由您来处理引用的对象消耗多少内存,以便取消引用和指针算法。

#4


2  

Void refers to the type. Basically the type of data that it points to is unknown.

Void是指类型。基本上它指向的数据类型是未知的。

Null refers to the value. It's essentially a pointer to nothing, and is invalid to use.

Null是指值。它本质上是一个指向nothing的指针,并且使用起来是无效的。

#5


2  

A null pointer points has the value NULL which is typically 0, but in any case a memory location which is invalid to dereference. A void pointer points at data of type void. The word "void" is not an indication that the data referenced by the pointer is invalid or that the pointer has been nullified.

空指针点的值为null,通常为0,但在任何情况下都是无效的内存位置。空指针指向void类型的数据。“void”一词并不是指指针引用的数据无效或指针已被取消。

#6


1  

Usually a null pointer (which can be of any type, including a void pointer !) points to:

通常一个空指针(可以是任何类型,包括空指针!)指向:

  • the address 0, against which most CPU instructions sets can do a very fast compare-and-branch (to check for uninitialized or invalid pointers, for instance) with optimal code size/performance for the ISA.

    地址0,大多数CPU指令集可以做一个非常快的比较和分支(例如检查未初始化的或无效的指针),并为ISA提供最优的代码大小/性能。

  • an address that's illegal for user code to access (such as 0x00000000 in many cases), so that if a code actually tries to access data at or near this address, the OS or debugger can easily stop or trap a program with this bug.

    对于用户代码访问(在许多情况下是0x00000000)的地址是非法的,因此如果一个代码实际上试图访问这个地址或附近的数据,操作系统或调试器可以很容易地停止或用这个bug捕获一个程序。

A void pointer is usually a method of cheating or turning-off compiler type checking, for instance if you want to return a pointer to one type, or an unknown type, to use as another type. For instance malloc() returns a void pointer to a type-less chunk of memory, the type of which you can cast to later use as a pointer to bytes, short ints, double floats, typePotato's, or whatever.

空指针通常是一种欺骗或关闭编译器类型检查的方法,例如,如果您想返回一个类型的指针,或者一个未知类型,作为另一种类型。例如,malloc()将一个空指针返回到一个类型较少的内存块中,您可以将其转换为一个指向字节、短int、double float、typePotato或其他的指针。

#7


1  

NULL is a value that is valid for any pointer type. It represents the absence of a value.

NULL是一个对任何指针类型都有效的值。它表示没有值。

A void pointer is a type. Any pointer type is convertible to a void pointer hence it can point to any value. This makes it good for general storage but bad for use. By itself it cannot be used to access a value. The program must have extra context to understand the type of value the void pointer refers to before it can access the value.

空指针是一种类型。任何指针类型都可以转换为空指针,因此它可以指向任何值。这使它对一般存储有利,但对使用不利。它本身不能用于访问一个值。程序必须有额外的上下文来理解空指针所指的值类型,然后才能访问该值。

#8


1  

Null pointers and void pointers are completely different from each other. If we request the operating system(through malloc() in c langauge) to allocate memory for a particular data type then the operating system allocates memory in heap (if space is available in heap) and sends the address of the memory which was allocated.

空指针和空指针完全不同。如果我们请求操作系统(通过malloc()在c语言中)为特定的数据类型分配内存,那么操作系统将在堆中分配内存(如果在堆中可用空间),并发送分配给内存的地址。

When memory is allocated by os in heap then we can assign this address value in any pointer type variable of that data type. This pointer is then called a void pointer until it is not taken for any process.

当内存由os在堆中分配时,我们可以将该地址值分配到该数据类型的任何指针类型变量中。然后这个指针被称为空指针,直到它没有被执行任何进程。

When the space is not available in heap then the operating system certainly allocates memory and sends an address value of that location but this memory is not allocated in heap by the os because there is no space in heap,in this case this memory is allocated by the os in the system memory.. This memory can not be accessed by the user hence when we assign this address value in a pointer then this pointer is known as null pointer, and we cannot use this pointer. In the case of void pointer we can use it for any process in any programming language.

当堆中可用的空间不那么肯定操作系统分配内存并发送一个地址值的位置,但这不是堆中分配的内存堆中的操作系统,因为没有空间,在这种情况下,这个操作系统的内存分配系统内存. .这个内存不能被用户访问,因此当我们在一个指针中分配这个地址值时,这个指针被称为空指针,我们不能使用这个指针。在void指针的情况下,我们可以在任何编程语言中使用它。

#9


0  

I don't think AnT's answer is correct.

我认为蚂蚁的回答不正确。

  1. NULL is just a pointer constant, otherwise how could we have ptr = NULL.
  2. NULL是一个指针常量,否则我们怎么能有ptr = NULL。
  3. As NULL is a pointer, what's its type. I think the type is just (void *), otherwise how could we have both int * ptr = NULL and (user-defined type)* ptr = NULL. void type is actually a universal type.
  4. NULL是指针,它的类型是什么。我认为类型只是(void *),否则我们如何同时拥有int * ptr = NULL和(用户定义类型)* ptr = NULL。void类型实际上是一个通用类型。
  5. Quoted in "C11(ISO/IEC 9899:201x) §6.3.2.3 Pointers Section 3":

    An integer constant expression with the value 0, or such an expression cast to type void *, is called a null pointer constant

    值为0的整数常量表达式,或将表达式转换为void *,称为空指针常量。

So simply put: NULL pointer is a void pointer constant.

简单地说,空指针是一个空指针常量。

#10


0  

Null pointer is a pointer which is not pointing to anything, it is used in cases where a pointer is not assigned a valid address in the memory. Every pointer type i.e int *, char * each have a null pointer value of its own.

空指针是一个指针,它没有指向任何东西,它用于在内存中没有指定一个有效地址的指针。我每一个指针类型。e int *, char *每个都有它自己的空指针值。

A void pointer is one which doesn't have any data type associated with it, i.e it can be assigned value of any type. Also known as the general purpose pointer, it is a C convention for a raw address.

空指针是一个没有任何与之相关的数据类型的指针。它可以被赋值任何类型。也称为通用指针,它是一个原始地址的C约定。

So, a null pointer is basically a null value assigned to a pointer of any data type whereas a void pointer is a data type which remains void as long as an address of a data type is not assigned to it.

因此,空指针基本上是赋给任何数据类型指针的空值,而空指针是一种数据类型,只要不分配数据类型的地址,它就仍然是空的。