Title: C + void pointer
Description: use of void pointer in C
FuerzaDelDios - November 21, 2006 04:58 AM (GMT)
Hi,
Can anyone plz let me know where do we use void pointers?
They cannot be derefrenced, nor can i perform any arithmetic operation on them. What are they used for?
Regs,
FDD
PinguinDude - November 21, 2006 12:58 PM (GMT)
Well, I used them in my list, in C.
| CODE |
typedef struct list_s list_t; struct list_s { list_t *parent; list_t *child; void *data; };
|
Because it is a void pointer it can point to anything.
This is perfect for a list :)
myork - November 21, 2006 03:27 PM (GMT)
A void* can be used when it is impossible to know at compile time the type of some data. This is very rare for normal programs, but when you write a library that other people may use it sometimes becomes necessary because you can not conceive all the uses of your library.
A good example is thread creation.
The person who created the thread library could not know what data you may want to pass to your thread at start-up. So the thread creation allows you to pass a void* that presumably the thread will know how to read.
| CODE |
void* myThread(void* data) { /* * This function assumes that the incoming data is a pointer to an int */ int myData = *(reinterpret_cast<int*>(data));
/* Do something thready */ return(NULL); }
int main() { pthread_t threadID; int threadData = 5;
pthread_create(&threadID,NULL,myThread,&threadData);
/* Do other stuff */ } |
Here the pthread_create() creator can not know what data is going to be passed to myThread(). But because the author of main() and myThread() will be the same person the data can be converted back into its original form.
NB List is not a good idea for the use of void* because you loose type information. The whole point of C/C++ being strongly typed is that the compiler will check that you do not use a type incorrectly.
A better idea would have been to use a template!
| CODE |
<template typename X> struct list_s { list_s *parent; list_s *child; X data; }; |
FuerzaDelDios - November 22, 2006 04:09 AM (GMT)
icon0x - November 23, 2006 07:45 AM (GMT)
myork your example compiles with g++ but when i run it, i keep gettin segmentation faults, n e idea what could be the issue?
myork - November 23, 2006 04:57 PM (GMT)
Yes.
The main thread is exiting before the created thread de-references the pointer.
The comment
| CODE |
| /* Do other stuff */ |
Is meant to indicate do some work that takes longer than the thread is alive.
icon0x - November 23, 2006 06:00 PM (GMT)
im still confused, wouldnt a getchar(); stop the main from exiting?
noteventime - November 24, 2006 10:43 PM (GMT)
You don't use void pointer. :lol:
Seriously, try to avoid them, they're nothing but trouble.
myork - November 25, 2006 08:42 AM (GMT)
| QUOTE (noteventime @ Nov 24 2006, 05:43 PM) |
You don't use void pointer. :lol: Seriously, try to avoid them, they're nothing but trouble. |
Yes stay away from void*.
As explained earlier it defeats the purpose of having a strongly typed language.
myork - November 25, 2006 08:45 AM (GMT)
| QUOTE (icon0x @ Nov 23 2006, 01:00 PM) |
| im still confused, wouldnt a getchar(); stop the main from exiting? |
Yes.
But I can tell what you are doing unless you post all the code. Guessing at what you have don wrong could take a long time.
Have you tried running it in gdb to see where the error is?
Start a new thread in the help section.