Home

voidpointer

A void pointer, written as void*, is a pointer type used in C and C++ that can hold the address of any object but does not have a specified data type. Because it has no type, a void* cannot be dereferenced directly or used in pointer arithmetic without first converting to a pointer to a concrete type.

In C, a void* acts as a generic object pointer. It can hold addresses of objects of

In C++, void* is also used as a generic pointer, but conversions between void* and other object

Common uses of void* include implementing generic data structures and APIs, such as buffers, opaque handles,

Limitations and pitfalls include the risk of incorrect casts, alignment issues, and violations of object lifetimes

Example: void* p = malloc(sizeof(int)); *(int*)p = 42; free(p);

any
type,
and
you
can
assign
between
object
pointers
and
void*
without
an
explicit
cast
in
either
direction.
To
access
the
pointed-to
data,
you
must
cast
the
void*
back
to
a
pointer
of
the
appropriate
type.
Pointer
arithmetic
is
not
allowed
on
void*
in
standard
C;
you
typically
cast
to
a
char*
or
to
the
target
type
before
performing
arithmetic.
Dereferencing
a
void*
requires
casting
to
the
correct
type
first.
pointer
types
are
not
implicit
and
require
explicit
casts
(such
as
static_cast
or
reinterpret_cast).
Dereferencing
still
requires
casting
to
a
concrete
type,
and
misuse
can
lead
to
undefined
behavior.
and
wrappers
around
memory
allocation
functions
(for
example,
malloc
in
C
returns
void*).
They
enable
type-agnostic
interfaces
but
at
the
cost
of
type
safety
and
potential
runtime
errors
if
the
actual
type
is
misidentified
or
miscast.
or
aliasing
rules.
Modern
code
often
minimizes
void*
usage
by
using
typed
wrappers
or
generic
constructs
where
available.