Home > Software design >  Why can't we use a void* to operate on the object it addresses
Why can't we use a void* to operate on the object it addresses

Time:04-14

I am learning C using C Primer 5th edition. In particular, i read about void*. There it is written that:

We cannot use a void* to operate on the object it addresses—we don’t know that object’s type, and the type determines what operations we can perform on that object.

void*: Pointer type that can point to any nonconst type. Such pointers may not be dereferenced.

My question is that if we're not allowed to use a void* to operate on the object it addressess then why do we need a void*. Also, i am not sure if the above quoted statement from C Primer is technically correct because i am not able to understand what it is conveying. Maybe some examples can help me understand what the author meant when he said that "we cannot use a void* to operate on the object it addresses". So can someone please provide some example to clarify what the author meant and whether he is correct or incorrect in saying the above statement.

CodePudding user response:

My question is that if we're not allowed to use a void* to operate on the object it addressess then why do we need a void*

It's indeed quite rare to need void* in C . It's more common in C.

But where it's useful is type-erasure. For example, try to store an object of any type in a variable, determining the type at runtime. You'll find that hiding the type becomes essential to achieve that task.

What you may be missing is that it is possible to convert the void* back to the typed pointer afterwards (or in special cases, you can reinterpret as another pointer type), which allows you to operate on the object.


Maybe some examples can help me understand what the author meant when he said that "we cannot use a void* to operate on the object it addresses"

Example:

int i;
int*  int_ptr  = &i;
void* void_ptr = &i;
 
*int_ptr = 42;  // OK
*void_ptr = 42; // ill-formed

As the example demonstrates, we cannot modify the pointed int object through the pointer to void.

so since a void* has no size(as written in the answer by PMF)

Their answer is misleading or you've misunderstood. The pointer has a size. But since there is no information about the type of the pointed object, the size of the pointed object is unknown. In a way, that's part of why it can point to an object of any size.

so how can a int* on the right hand side be implicitly converted to a void*

All pointers to objects can implicitly be converted to void* because the language rules say so.

CodePudding user response:

Yes, the author is right.

A pointer of type void* cannot be dereferenced, because it has no size1. The compiler would not know how much data he needs to get from that address if you try to access it:

void* myData = std::malloc(1000); // Allocate some memory (note that the return type of malloc() is void*)

int value = *myData; // Error, can't dereference
int field = myData->myField; // Error, a void pointer obviously has no fields

The first example fails because the compiler doesn't know how much data to get. We need to tell it the size of the data to get:

int value = *(int*)myData; // Now fine, we have casted the pointer to int*
int value = *(char*)myData; // Fine too, but NOT the same as above!

or, to be more in the C -world:

int value = *static_cast<int*>(myData);
int value = *static_cast<char*>(myData);

The two examples return a different result, because the first gets an integer (32 bit on most systems) from the target address, while the second only gets a single byte and then moves that to a larger variable.

The reason why the use of void* is sometimes still useful is when the type of data doesn't matter much, like when just copying stuff around. Methods such as memset or memcpy take void* parameters, since they don't care about the actual structure of the data (but they need to be given the size explicitly). When working in C (as opposed to C) you'll not use these very often, though.

1 "No size" applies to the size of the destination object, not the size of the variable containing the pointer. sizeof(void*) is perfectly valid and returns, the size of a pointer variable. This is always equal to any other pointer size, so sizeof(void*)==sizeof(int*)==sizeof(MyClass*) is always true (for 99% of today's compilers at least). The type of the pointer however defines the size of the element it points to. And that is required for the compiler so he knows how much data he needs to get, or, when used with or -, how much to add or subtract to get the address of the next or previous elements.

CodePudding user response:

void * is basically a catch-all type. Any pointer type can be implicitly cast to void * without getting any errors. As such, it is mostly used in low level data manipulations, where all that matters is the data that some memory block contains, rather than what the data represents. On the flip side, when you have a void * pointer, it is impossible to determine directly which type it was originally. That's why you can't operate on the object it addresses.

if we try something like

typedef struct foo {
   int key;
   int value;
}              t_foo;

void try_fill_with_zero(void *destination) {
    destination->key = 0;
    destination->value = 0;
}

int main() {
    t_foo *foo_instance = malloc(sizeof(t_foo));
    
    try_fill_with_zero(foo_instance, sizeof(t_foo));
}

we will get a compilation error because it is impossible to determine what type void *destination was, as soon as the address gets into try_fill_with_zero. That's an example of being unable to "use a void* to operate on the object it addresses"

Typically you will see something like this:

typedef struct foo {
   int key;
   int value;
}              t_foo;

void init_with_zero(void *destination, size_t bytes) {
    unsigned char *to_fill = (unsigned char *)destination;
    
    for (int i = 0; i < bytes; i  ) {
         to_fill[i] = 0;
    }
}

int main() {
    t_foo *foo_instance = malloc(sizeof(t_foo));
    int   test_int;
    
    init_with_zero(foo_instance, sizeof(t_foo));
    init_with_zero(&test_int, sizeof(int));
}

Here we can operate on the memory that we pass to init_with_zero represented as bytes.

CodePudding user response:

You can think of void * as representing missing knowledge about the associated type of the data at this address. You may still cast it to something else and then dereference it, if you know what is behind it. Example:

int n = 5;
void * p = (void *) &n;

At this point, p we have lost the type information for p and thus, the compiler does not know what to do with it. But if you know this p is an address to an integer, then you can use that information:

int * q = (int *) p;
int m = *q;

And m will be equal to n.

void is not a type like any other. There is no object of type void. Hence, there exists no way of operating on such pointers.

CodePudding user response:

This is one of my favourite kind of questions because at first I was also so confused about void pointers.

Like the rest of the Answers above void * refers to a generic type of data. Being a void pointer you must understand that it only holds the address of some kind of data or object.

No other information about the object itself, at first you are asking yourself why do you even need this if it's only able to hold an address. That's because you can still cast your pointer to a more specific kind of data, and that's the real power.

Making generic functions that works with all kind of data. And to be more clear let's say you want to implement generic sorting algorithm. The sorting algorithm has basically 2 steps:

  1. The algorithm itself.
  2. The comparation between the objects.

Here we will also talk about pointer functions.

Let's take for example qsort built in function

void qsort(void *base, size_t nitems, size_t size, int (*compar)(const void *, const void*))

We see that it takes the next parameters:

base − This is the pointer to the first element of the array to be sorted.

nitems − This is the number of elements in the array pointed by base.

size − This is the size in bytes of each element in the array.

compar − This is the function that compares two elements.

And based on the article that I referenced above we can do something like this:

int values[] = { 88, 56, 100, 2, 25 };

int cmpfunc (const void * a, const void * b) {
   return ( *(int*)a - *(int*)b );
}

int main () {
   int n;

   printf("Before sorting the list is: \n");
   for( n = 0 ; n < 5; n   ) {
      printf("%d ", values[n]);
   }

   qsort(values, 5, sizeof(int), cmpfunc);

   printf("\nAfter sorting the list is: \n");
   for( n = 0 ; n < 5; n   ) {   
      printf("%d ", values[n]);
   }
  
   return(0);
}

Where you can define your own custom compare function that can match any kind of data, there can be even a more complex data structure like a class instance of some kind of object you just define. Let's say a Person class, that has a field age and you want to sort all Persons by age.

And that's one example where you can use void * , you can abstract this and create other use cases based on this example.

It is true that is a C example, but I think, being something that appeared in C can make more sense of the real usage of void *. If you can understand what you can do with void * you are good to go.

  • Related