Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

A void pointer (void*), also known as a generic pointer, is a special type of pointer that holds a memory address but has no associated data type. Because it lacks type information, the compiler does not know the size of the data being pointed to or how to interpret its underlying bit pattern. A void* can implicitly accept the memory address of any non-const, non-volatile object without requiring a cast.
int intValue = 42;
double doubleValue = 3.14;

void* voidPtr;
voidPtr = &intValue;    // Valid: implicitly converts int* to void*
voidPtr = &doubleValue; // Valid: implicitly converts double* to void*

Dereferencing Restrictions

A void pointer cannot be dereferenced directly. Because the compiler does not know the type, it cannot determine how many bytes to read from memory or how to format those bytes. To access the underlying data, the void pointer must be explicitly cast back to a pointer of the correct concrete type. In C++, static_cast is the standard mechanism for this operation.
int value = 100;
void* ptr = &value;

// int result = *ptr; // COMPILER ERROR: 'void*' is not a pointer-to-object type

// Correct C++ casting
int* intPtr = static_cast<int*>(ptr);
int result = *intPtr; // Valid: result is 100

Pointer Arithmetic

Pointer arithmetic (e.g., ptr++, ptr + 1) is strictly prohibited on void pointers in standard C++. Pointer arithmetic relies on the sizeof the pointed-to type to calculate memory offsets. Since void is an incomplete type with no defined size, the compiler cannot compute the byte offset required to advance the pointer.
int arr[3] = {10, 20, 30};
void* ptr = arr;

// ptr++; // COMPILER ERROR: arithmetic on a pointer to void

// Must cast to a typed pointer before performing arithmetic
int* intPtr = static_cast<int*>(ptr);
intPtr++; // Valid: advances the memory address by sizeof(int) bytes

CV-Qualification (Const and Volatile)

A standard void* cannot hold the address of a const or volatile qualified variable. Allowing this would strip the qualifier and violate const-correctness. To store the address of a constant object, you must use a const void*.
const int constValue = 50;

// void* ptr = &constValue; // COMPILER ERROR: invalid conversion from 'const int*' to 'void*'

const void* constPtr = &constValue; // Valid

// When casting back, the const qualifier must be preserved
const int* typedConstPtr = static_cast<const int*>(constPtr);

Type Safety Caveat

While C++ enforces strict type checking during the static_cast syntax, it cannot verify at runtime if the void* actually points to the type you are casting it to. Casting a void* to an incorrect type and dereferencing it results in undefined behavior.
float floatValue = 9.81f;
void* ptr = &floatValue;

// Undefined Behavior: Casting to the wrong type
int* wrongPtr = static_cast<int*>(ptr); 
int garbageValue = *wrongPtr; 
Master C++ with Deep Grasping Methodology!Learn More