C语言 空指针和空指针有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3581585/
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
What's the difference between a null pointer and a void pointer?
提问by Pavitar
Whats the difference between a Null pointer& a Void pointer?
请告诉我之间的差别空指针和一个空指针?
回答by AnT
Null pointeris a special reserved valueof 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 pointeris a specific pointer type- void *- a pointer that points to some data location in storage, which doesn't have any specific type.
空指针是一种特定的指针类型——void *指向存储中某个数据位置的指针,它没有任何特定的类型。
So, once again, null pointeris a value, while void pointeris 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?".
所以,再一次,空指针是一个值,而空指针是一个类型。这些概念完全不同,没有可比性。这基本上意味着您的问题,如上所述,并不完全有效。这就像问,例如,“三角形和汽车之间有什么区别?”。
回答by vanza
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 指针”是一种类型(void *)。“空指针”是具有零值(NULL)的指针。例子:
void *pointer = NULL;
That's a NULL void pointer.
那是一个 NULL void 指针。
回答by aaronasterling
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 intpointer you would do
空指针保证不等于指向任何对象的指针。它的实际值取决于系统,可能因类型而异。要获得一个空int指针,你会这样做
int* p = 0;
A null pointer will be returned by mallocon failure.
malloc失败时将返回空指针。
We can test if a pointer is null, i.e. if mallocor 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.
void 指针可以指向任何类型,并且由您来处理被引用对象为取消引用和指针运算而消耗的内存量。
回答by Shawn D.
Void refers to the type. Basically the type of data that it points to is unknown.
空是指类型。基本上它指向的数据类型是未知的。
Null refers to the value. It's essentially a pointer to nothing, and is invalid to use.
Null 是指值。它本质上是一个指向空的指针,不能使用。
回答by Jesse Dhillon
A null pointer points has the value NULLwhich 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 类型的数据。“void”一词并不表示指针引用的数据无效或指针已被取消。
回答by hotpaw2
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.
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.
地址 0,大多数 CPU 指令集可以针对该地址进行非常快速的比较和分支(例如检查未初始化或无效的指针),并具有 ISA 的最佳代码大小/性能。
用户代码访问非法的地址(例如在许多情况下为 0x00000000),因此如果代码实际上尝试访问此地址处或附近的数据,操作系统或调试器可以轻松地停止或捕获具有此错误的程序。
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.
void 指针通常是一种欺骗或关闭编译器类型检查的方法,例如,如果您想返回指向一种类型或未知类型的指针以用作另一种类型。例如 malloc() 返回一个指向无类型内存块的空指针,您可以将其类型转换为以后用作指向字节、短整数、双浮点数、typePotato 或其他任何内容的指针。
回答by JaredPar
NULLis 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.
空指针是一种类型。任何指针类型都可以转换为 void 指针,因此它可以指向任何值。这使它适合一般存储,但不适合使用。它本身不能用于访问值。程序必须有额外的上下文来理解 void 指针所指的值的类型,然后才能访问该值。
回答by Ghulam Rabbani
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.
空指针和空指针完全不同。如果我们请求操作系统(通过 c 语言中的 malloc() )为特定数据类型分配内存,则操作系统在堆中分配内存(如果堆中有可用空间)并发送分配的内存地址。
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 指针的情况下,我们可以将它用于任何编程语言中的任何进程。
回答by nathan
I don't think AnT's answeris correct.
我不认为AnT的答案是正确的。
NULLis just a pointer constant, otherwise how could we haveptr = NULL.- As
NULLis a pointer, what's its type. I think the type is just(void *), otherwise how could we have bothint * ptr = NULLand(user-defined type)* ptr = NULL.voidtype is actually a universal type. - 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
NULL只是一个指针常量,否则我们怎么会有ptr = NULL.- 作为
NULL一个指针,它的类型是什么。我认为类型只是(void *),否则我们怎么可能同时拥有int * ptr = NULL和(user-defined type)* ptr = NULL。voidtype 实际上是一个通用类型。 - 在“C11(ISO/IEC 9899:201x) §6.3.2.3 指针第 3 节”中引用:
值为 0 的整数常量表达式,或转换为 void * 类型的此类表达式,称为空指针常量
So simply put: NULLpointer is a void pointer constant.
所以简单地说:NULL指针是一个空指针常量。
回答by Akshat Chhangani
A Null pointer has the value 0. void pointer is a generic pointer introduced by ANSI. Generic pointer can hold the address of any data type.
Null 指针的值为 0。void 指针是 ANSI 引入的通用指针。通用指针可以保存任何数据类型的地址。

