What's the difference between a null pointer and a void pointer?
Whats the difference between a Nu开发者_Python百科ll pointer & a Void pointer?
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.
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.
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?".
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 *pointer = NULL;
That's a NULL void pointer.
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* p = 0;
A null pointer will be returned by malloc
on failure.
We can test if a pointer is null, i.e. if malloc
or some other function failed simply by testing its boolean value:
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 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.
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.
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.
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.
NULL
is a value that is valid for any pointer type. It represents the absence of a value.
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.
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.
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.
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.
Both void
and null
pointers point to a memory location in the end.
A null
pointer points to place (in a memory segment) which is usually the 0th address of memory segment. It is almost all about how the underlying OS treat that "reserved" memory location (an implementation detail) when you try to access it to read/write or dereference. For example, that particular memory location can be marked as "non-accessible" and throws an expection when it's accessed.
A void
pointer can point to anywhere and potentially represent any type (primitive, reference-type, and including null
).
As someone nicely put above, null
pointer represents a value whereas void*
represents a type more than a value.
I don't think AnT's answer is correct.
NULL
is just a pointer constant, otherwise how could we haveptr = NULL
.- As
NULL
is a pointer, what's its type. I think the type is just(void *)
, otherwise how could we have bothint * ptr = NULL
and(user-defined type)* ptr = NULL
.void
type 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
So simply put: NULL
pointer is a void pointer constant.
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.
精彩评论