Archived

This topic is now archived and is closed to further replies.

what is a void* pointer?

This topic is 6159 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

void*''s are often used in structures and stuff when you may need some extra memory for whatever. Then you can malloc() some memory of whatever data type you need and then free() it when you''re down. It''s very verstile because your not stuck with a single data type or a fixed amount of data.



"We are the music makers, and we are the dreamers of the dreams."
- Willy Wonka

Share this post


Link to post
Share on other sites
A void* pointer is basically a pointer of indeterminate type, or an address without a type setting, however you want to look at it. This works because all pointers are the same size. Think about that for a minute... the data that the pointer points to may be of different sizes, but the actual physical size in memory of the pointer itself, which is just an address, has a fixed length dependent on operating system. Therefore the compiler lets you pass pure addresses using this mechanism. This is both a powerful and a scary thing, because you are required to make sense of what that pointer is pointing at when you reach the other side. It''s your responsibility to make sure things match up, and if they don''t, you''ll have memory overwrites and all sorts of nasty stuff going on, and you won''t get any warnings until it fatals.

-fel

Share this post


Link to post
Share on other sites
Thank you both! I have benn wondering what that was all about for a few days. Now if i wanted to use a void pointer in a class or whatever, and wanted to put an int in it, would i allocate memory for the type of data I wanted to contain, i.e.

void* void_pointer;
void_pointer = new int;
*void pointer = 2;
delete void_pointer;

right?

Edited by - CTRL_ALT_DELETE on January 31, 2001 5:12:54 PM

Share this post


Link to post
Share on other sites
I''m not a C++ guy by nature, but I''m pretty sure that is correct.
Just for the sake of it, the C equivelent would be:

void *void_pointer;

void_pointer = malloc(sizeof(int));

*void_pointer = 2;

free(void_pointer);

notably, I have missed out all error checking.

Also, on the subject - do I need any cast for the assignment operation (*void_pointer = 2'')?

-Mezz

Share this post


Link to post
Share on other sites


Nope. Do not do that. It might work but it might make you soil your pants one day as well.

If you want an int:

int * temp=new int;
void* vptr=temp;

to get your int ptr back do the following:

int* temp2=static_cast(vptr);

or sometimes a reinterpret_cast(vptr); is required( I try to avoid casts).

Please for the sake of your underware, do not use void ptrs unless you NEED to. Void ptrs are really only usefull in Low level programming, like operating systems or memory managers.

If you don''t already know, learn about templates, and polymorphism, they will help you get rid of 99.999% of your void pointer requirements, and are type safe.

send me an email if you think I am a retard, or if you wan''t to tell my I am a genius(or anything in between) animade@mailcity.com

Share this post


Link to post
Share on other sites
A good example of return a void* for a function is when you lock a DX surface. Depending on the bit depth of the surface, you may be getting a UCHAR, SHORT, or INT data type for the memory. For example, the lpSurface member in the DDSURFACEDESC2 structure is of type void* for this reason. Wden you lock the surface, you typecast the returned void pointer to whatever type you need.

Share this post


Link to post
Share on other sites
Well Mr Brown, you would hsve to type cast the void pointer first. i.e. you void pointer''s reference has no determined size without a cast. So the compiler would not know whether to add 1, 2, 4, 10, or whatever number of bytes to find the next reference.
That''s one of the dangers of void pointers.

By the way they are usually used in C for emulate concepts such as object oriented code a function.(Saw it in a book once at the library.)



It''s me, it''s that T-H-E-D.

Share this post


Link to post
Share on other sites
I wrote a loader for a simple graphics format - and it used a void pointer. After the file header, all I had to do was allocate a buffer, read the data in, make a call to SetDIBitsToDevice, and free the data. So I used a void pointer.

I could have made it a typed pointer, but all that would mean was I had to typecast the result of malloc.

Share this post


Link to post
Share on other sites