Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a void pointer in C++? [duplicate]

Possible Duplicate:
What is a void pointer and what is a null pointer?

I often see code which resembles something like the following:

void * foo(int bar);

What does this mean? Does it mean that it can return anything? Is this similar to dynamic or object in C#?

like image 361
zeboidlund Avatar asked Dec 16 '11 05:12

zeboidlund


People also ask

What is a void pointer C?

The void pointer in C is a pointer that is not associated with any data types. It points to some data location in the storage. This means that it points to the address of variables. It is also called the general purpose pointer. In C, malloc() and calloc() functions return void * or generic pointers.

What does void * mean in C?

void* is a "pointer to anything". void ** is another level of indirection - "pointer to pointer to anything". Basically, you pass that in when you want to allow the function to return a pointer of any type.

What is void pointer example?

Example of pointer in Cint a=5; int* point = &a; // pointer variable point is pointing to the address of the integer variable a! int a=5; int* point = &a; // pointer variable point is pointing to the address of the integer variable a!

Can we get Typeid of void pointer?

You can use the strcmp function to help you compare const char* strings. Then after that you called GetType function, now you know to which type to convert or cast the void pointer of the Object and then do whatever you want with the current iterated object!


1 Answers

A void* does not mean anything. It is a pointer, but the type that it points to is not known.

It's not that it can return "anything". A function that returns a void* generally is doing one of the following:

  • It is dealing in unformatted memory. This is what operator new and malloc return: a pointer to a block of memory of a certain size. Since the memory does not have a type (because it does not have a properly constructed object in it yet), it is typeless. IE: void.
  • It is an opaque handle; it references a created object without naming a specific type. Code that does this is generally poorly formed, since this is better done by forward declaring a struct/class and simply not providing a public definition for it. Because then, at least it has a real type.
  • It returns a pointer to storage that contains an object of a known type. However, that API is used to deal with objects of a wide variety of types, so the exact type that a particular call returns cannot be known at compile time. Therefore, there will be some documentation explaining when it stores which kinds of objects, and therefore which type you can safely cast it to.

This construct is nothing like dynamic or object in C#. Those tools actually know what the original type is; void* does not. This makes it far more dangerous than any of those, because it is very easy to get it wrong, and there's no way to ask if a particular usage is the right one.

And on a personal note, if you see code that uses void*'s "often", you should rethink what code you're looking at. void* usage, especially in C++, should be rare, used primary for dealing in raw memory.

like image 147
Nicol Bolas Avatar answered Oct 02 '22 02:10

Nicol Bolas