Whats the difference between a Null pointer & a Void pointer?
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.
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.
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.
I don't think AnT's answer is correct.
NULL
is just a pointer constant, otherwise how could we have ptr = NULL
. 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.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 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.