Computer Science Canada

void *

Author:  klopyrev [ Sat Mar 24, 2007 5:06 pm ]
Post subject:  void *

Can someone please explain to me the purpose of void *? Also, why would your ever have code like:

typedef void (*terminate_handler) ();

I saw code like this in the header file exception and it really confuses me. Also, I keep reading about how void * allow versatility and such. What does that mean?

KL

Author:  wtd [ Sat Mar 24, 2007 5:19 pm ]
Post subject:  RE:void *

Void pointers carry no type data. They can be a pointer to anything. This is both powerful and dangerous.

In a language without any support for generic programming, it is also sometimes necessary. Consider a linked list implementation that can store any type of data. In order to achieve this, we'd need a node similar to the following.

code:
struct Node
{
   struct Node * next;
   void * data;
};


The "data" member can then hold a pointer to anything. Typecasts would of course be necessary, and improperly casting could result in run-time errors that are exceptionally difficult to track. Thus, this is a dangerous technique.

IN a language with support for generic programming (like C++), we would simple write the following.

code:
template <typename T>
struct Node
{
   Node * next;
   T * data;
};


This is type-safe. When we need a linked list that stores ints, the compiler simply generates a new struct for us. There is no need for casts.

Author:  OneOffDriveByPoster [ Sat Mar 24, 2007 5:23 pm ]
Post subject:  Re: void *

klopyrev @ Sat Mar 24, 2007 5:06 pm wrote:
why would your ever have code like:

typedef void (*terminate_handler) ();
That says that a terminate_handler is a pointer to function taking no arguments and returning nothing.
You could pass pointers to functions so that the function pointed to can get called later (as a callback function).

Quote:
I keep reading about how void * allow versatility and such. What does that mean?
A void* can be used to point to an object of any type. For fixed function signatures (like in callback functions), it may be useful for there to be a void* parameter which could be an object of any type. The code that registers the callback can provide additional information to the callback function through the void* (both the callback function and the code that registers it will "know" the type involved).

Author:  klopyrev [ Sat Mar 24, 2007 7:38 pm ]
Post subject:  Re: void *

What I don't get about the typedef statement is what exactly is the name and what is the type it is being assigned. I'm used to seeing something like:

typedef char BYTE;

BYTE is clearly becoming a char.

In

typedef void (*terminate_handler) ();

its a bit confusing.

KL

Author:  OneOffDriveByPoster [ Sun Mar 25, 2007 9:17 am ]
Post subject:  Re: void *

klopyrev @ Sat Mar 24, 2007 7:38 pm wrote:
In

typedef void (*terminate_handler) ();

its a bit confusing.
What the type and the name is has been stated in my previous post (although it may not be the most clear).

Consider:
code:
extern
char *pc;  // pointer to char
char (*pc);  // pointer to char
char arr[5];  // array of 5 char
char (*parr)[5];  // pointer to array of 5 char
char f();  // function returning char
char (f)();  // function returning char
char (*fp)();  // pointer to function returning char

Author:  wtd [ Sun Mar 25, 2007 9:39 am ]
Post subject:  RE:void *

The parens are necessary because otherwise it's a function returning a char pointer.

Author:  klopyrev [ Sun Mar 25, 2007 3:37 pm ]
Post subject:  Re: void *

Thanks! I understand it now. Coming from Java, C++ does seem a bit confusing at times Razz

KL

Author:  Null [ Mon Mar 26, 2007 3:53 pm ]
Post subject:  RE:void *

Keep in mind that you'll mostly see void pointers in C rather than C++ for reasons wtd mentioned before.


: