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

4

117

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#?

Alcaraz answered 16/12, 2011 at 5:14 Comment(3)
A link to similar question: #4335331Dendrite
It has no relationship with dynamic and is only a gross "similarity" with object.Kwarteng
@ChrisFarr You can't reopen this question by deleting the duplicate notice. It would just hide which question this was closed against. Consider creating a post on meta if you feel this should be reopened.Georgena
I
154

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.

Indifferentism answered 16/12, 2011 at 5:24 Comment(0)
P
42

Void is used as a keyword. The void pointer, also known as the generic pointer, is a special type of pointer that can be pointed at objects of any data type! A void pointer is declared like a normal pointer, using the void keyword as the pointer’s type:

General syntax:

void* pointer_variable;

void *pVoid; // pVoid is a void pointer

A void pointer can point to objects of any data type:

int nValue;
float fValue;

struct Something
{
    int nValue;
    float fValue;
};

Something sValue;

void *pVoid;
pVoid = &nValue; // valid
pVoid = &fValue; // valid
pVoid = &sValue; // valid

However, because the void pointer does not know what type of object it is pointing to, it can not be dereferenced! Rather, the void pointer must first be explicitly cast to another pointer type before it is dereferenced.

int nValue = 5;
void *pVoid = &nValue;

// can not dereference pVoid because it is a void pointer

int *pInt = static_cast<int*>(pVoid); // cast from void* to int*

cout << *pInt << endl; // can dereference pInt

Source: link

Palmette answered 16/12, 2011 at 5:23 Comment(5)
In C++ those last three lines would cause a compilation error because you have to explicitly cast things to void*, unlike C .Triform
Code example currently works thank you. To me this question and top 2 answers are more concise and pointed than the linked "duplicate".Usn
It is necessary to explicitly state that the function pointer is an exception for the void pointer!Marindamarinduque
@SethCarnegie is p=&nValue and then cout<<*(int*)p right?Poky
@SethCarnegie There's no compilation error, could you please explain what the problem is? And... OP, please mention the answer is from another source (now it's edited by another user, but it took 7 years...).Quanta
R
24

A void* pointer is used when you want to indicate a pointer to a hunk of memory without specifying the type. C's malloc returns such a pointer, expecting you to cast it to a particular type immediately. It really isn't useful until you cast it to another pointer type. You're expected to know which type to cast it to, the compiler has no reflection capability to know what the underlying type should be.

Reimers answered 16/12, 2011 at 5:18 Comment(2)
Well, C's malloc doesn't actually expect you to cast it to something else immediately, because in C, void* gets promoted to a real pointer type automatically: https://mcmap.net/q/21631/-should-i-cast-the-result-of-malloc-in-c/837703. C++ is a different story, but you're supposed to be using new in C++ anyway.Tameshatamez
@ArkaMajumdar an implicit cast is still a cast. The type of the pointer changes, which is what allows it to be useful.Reimers
P
3

A void* can point to anything (it's a raw pointer without any type info).

Pinot answered 16/12, 2011 at 5:15 Comment(2)
Any pointer can point to anything.Triform
@AJ.S Any pointer can refer to any location in memory https://mcmap.net/q/189248/-any-type-of-pointer-can-point-to-anythingHermia

© 2022 - 2024 — McMap. All rights reserved.